Skip to content

Commit

Permalink
clang-tidy: add more modernize-* checks (#71)
Browse files Browse the repository at this point in the history
* clang-tidy: add more modernize-* checks

* pre-commit: add a custom hook for clang-tidy

* clang-tidy enable more readability-* checks
  • Loading branch information
ManuelHu authored Apr 16, 2024
1 parent 0691f18 commit f0df7ca
Show file tree
Hide file tree
Showing 29 changed files with 101 additions and 99 deletions.
31 changes: 20 additions & 11 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
---
Checks: "-*, \
modernize-deprecated-headers, \
modernize-make-shared, \
modernize-make-unique, \
modernize-redundant-void-arg, \
modernize-use-auto, \
modernize-use-equals-default, \
modernize-use-nullptr, \
modernize-use-using, \
modernize-*, \
-modernize-avoid-c-arrays, \
-modernize-pass-by-value, \
-modernize-use-trailing-return-type, \
performance-faster-string-find, \
performance-for-range-copy, \
performance-inefficient-string-concatenation, \
performance-trivially-destructible, \
readability-delete-null-pointer, \
readability-duplicate-include, \
readability-string-compare"
readability-*, \
-readability-magic-numbers, \
-readability-convert-member-functions-to-static, \
-readability-isolate-declaration, \
-readability-implicit-bool-conversion, \
-readability-else-after-return, \
-readability-braces-around-statements, \
-readability-named-parameter, \
-readability-identifier-length, \
-readability-qualified-auto, \
-readability-function-cognitive-complexity"

CheckOptions:
- key: modernize-use-override.IgnoreDestructors
value: true

FormatStyle: file
HeaderFilterRegex: 'include/.*.hh'
10 changes: 9 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"

exclude: ^include/(fmt|magic_enum|CLI11)/
exclude: ^include/(fmt|magic_enum|CLI11|EcoMug)/
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand Down Expand Up @@ -32,6 +32,14 @@ repos:
- id: codespell
args: ["-L", "hist"]

- repo: local
hooks:
- id: clang-tidy
name: clang-tidy
entry: bash -c 'command -V clang-tidy >&- 2>&-; if [[ $? == 0 && -d build ]]; then clang-tidy -fix -fix-errors -quiet -p=build "$@"; fi'
language: system
types_or: [c++]

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.3
hooks:
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ set(CMAKE_INSTALL_MESSAGE LAZY)
# Only allow installs relative to CMAKE_INSTALL_PREFIX
set(CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION ON)

# export compile commands, e.g. for use by clang-tidy.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Set default build type
include(BuildType)

Expand Down
2 changes: 1 addition & 1 deletion examples/02-hpge/HPGeTestStand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ G4VPhysicalVolume* HPGeTestStand::DefineGeometry() {

auto world_l = new G4LogicalVolume(world_s, LAr, "WorldLAr");

auto world_p = new G4PVPlacement(nullptr, G4ThreeVector(), world_l, "World", 0, false, 0);
auto world_p = new G4PVPlacement(nullptr, G4ThreeVector(), world_l, "World", nullptr, false, 0);

auto hpge_s = new G4Box("HPGe", 5 * u::cm, 5 * u::cm, 5 * u::cm);

Expand Down
2 changes: 1 addition & 1 deletion examples/02-hpge/HPGeTestStand.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class HPGeTestStand : public RMGHardware {

public:

G4VPhysicalVolume* DefineGeometry();
G4VPhysicalVolume* DefineGeometry() override;
};

#endif
2 changes: 1 addition & 1 deletion examples/04-cosmogenics/HPGeTestStand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ G4VPhysicalVolume* HPGeTestStand::DefineGeometry() {

auto world_l = new G4LogicalVolume(world_s, LAr, "WorldLAr");

auto world_p = new G4PVPlacement(nullptr, G4ThreeVector(), world_l, "World", 0, false, 0);
auto world_p = new G4PVPlacement(nullptr, G4ThreeVector(), world_l, "World", nullptr, false, 0);

auto hpge_s = new G4Box("HPGe", 5 * u::cm, 5 * u::cm, 5 * u::cm);

Expand Down
2 changes: 1 addition & 1 deletion examples/04-cosmogenics/HPGeTestStand.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class HPGeTestStand : public RMGHardware {

public:

G4VPhysicalVolume* DefineGeometry();
G4VPhysicalVolume* DefineGeometry() override;
};

#endif
1 change: 0 additions & 1 deletion include/RMGEventAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

class RMGRunAction;
class G4GenericMessenger;
class RMGVOutputManager;
class RMGEventAction : public G4UserEventAction {

public:
Expand Down
2 changes: 1 addition & 1 deletion include/RMGGermaniumDetector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>

#include "G4Allocator.hh"
#include "G4GenericMessenger.hh"
#include "G4THitsCollection.hh"
#include "G4ThreeVector.hh"
#include "G4VHit.hh"
Expand Down Expand Up @@ -56,7 +57,6 @@ using RMGGermaniumDetectorHitsCollection = G4THitsCollection<RMGGermaniumDetecto
class G4Step;
class G4HCofThisEvent;
class G4TouchableHistory;
class G4GenericMessenger;
class RMGGermaniumDetector : public G4VSensitiveDetector {

public:
Expand Down
2 changes: 1 addition & 1 deletion include/RMGHardware.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RMGHardware : public G4VUserDetectorConstruction {

void RegisterDetector(DetectorType type, const std::string& pv_name, int uid, int copy_nr = 0);
inline const auto& GetDetectorMetadataMap() { return fDetectorMetadata; }
inline const auto& GetDetectorMetadata(std::pair<std::string, int> det) {
inline const auto& GetDetectorMetadata(const std::pair<std::string, int>& det) {
return fDetectorMetadata.at(det);
}
inline const auto& GetActiveDetectorList() { return fActiveDetectors; }
Expand Down
2 changes: 1 addition & 1 deletion include/RMGHardwareMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RMGHardwareMessenger : public G4UImessenger {
RMGHardwareMessenger(RMGHardware* hw);
~RMGHardwareMessenger();

void SetNewValue(G4UIcommand* command, G4String newValues);
void SetNewValue(G4UIcommand* command, G4String newValues) override;

private:

Expand Down
4 changes: 2 additions & 2 deletions include/RMGLog.hh
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class RMGLog {
* Writes string to the screen log if the log level is equal or greater than the minimum
* @param loglevel loglevel for the current message
* @param message string to write to the screen log */
template<typename T> static void Out(RMGLog::LogLevel loglevel, const T& msg);
template<typename T> static void Out(RMGLog::LogLevel loglevel, const T& message);

template<typename T, typename... Args>
static void Out(RMGLog::LogLevel loglevel, const T& msg_first, const Args&... msg_other);
static void Out(RMGLog::LogLevel loglevel, const T& msg_first, const Args&... args);

template<typename... Args>
static void OutFormat(RMGLog::LogLevel loglevel, const std::string& fmt, const Args&... args);
Expand Down
6 changes: 3 additions & 3 deletions include/RMGManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class RMGManager {
G4VisManager* GetG4VisManager();
RMGHardware* GetDetectorConstruction();
G4VUserPhysicsList* GetProcessesList();
inline int GetPrintModulo() { return fPrintModulo; }
[[nodiscard]] inline int GetPrintModulo() const { return fPrintModulo; }

inline bool IsExecSequential() {
return fG4RunManager->GetRunManagerType() == G4RunManager::RMType::sequentialRM;
}
inline bool IsPersistencyEnabled() { return fIsPersistencyEnabled; }
[[nodiscard]] inline bool IsPersistencyEnabled() const { return fIsPersistencyEnabled; }
inline const std::string& GetOutputFileName() { return fOutputFile; }

// setters
Expand All @@ -83,7 +83,7 @@ class RMGManager {
void SetRandEngineSeed(long seed);
void SetRandEngineInternalSeed(int index);
void SetRandSystemEntropySeed();
inline bool GetRandIsControlled() { return fIsRandControlled; }
[[nodiscard]] inline bool GetRandIsControlled() const { return fIsRandControlled; }

void SetLogLevel(std::string level);

Expand Down
6 changes: 3 additions & 3 deletions include/RMGMasterGenerator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RMGMasterGenerator : public G4VUserPrimaryGeneratorAction {

inline RMGVGenerator* GetGenerator() { return fGeneratorObj.get(); }
inline RMGVVertexGenerator* GetVertexGenerator() { return fVertexGeneratorObj.get(); }
inline Confinement GetConfinement() const { return fConfinement; }
[[nodiscard]] inline Confinement GetConfinement() const { return fConfinement; }

void SetConfinement(Confinement code);
void SetConfinementString(std::string code);
Expand All @@ -65,10 +65,10 @@ class RMGMasterGenerator : public G4VUserPrimaryGeneratorAction {

private:

Confinement fConfinement;
Confinement fConfinement{RMGMasterGenerator::Confinement::kUnConfined};
std::unique_ptr<RMGVVertexGenerator> fVertexGeneratorObj;

Generator fGenerator;
Generator fGenerator{RMGMasterGenerator::Generator::kUndefined};
std::unique_ptr<RMGVGenerator> fGeneratorObj;

std::unique_ptr<G4GenericMessenger> fMessenger;
Expand Down
2 changes: 1 addition & 1 deletion include/RMGPhysics.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class RMGPhysics : public G4VModularPhysicsList {
void SetPhysicsRealmString(std::string realm);
void SetLowEnergyEMOptionString(std::string option);
void SetUseGammaAngCorr(bool);
void SetGammaTwoJMAX(int two_j_max);
void SetGammaTwoJMAX(int max_two_j);
void SetStoreICLevelData(bool);

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/RMGRun.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RMGRun : public G4Run {

using TimePoint = std::chrono::time_point<std::chrono::system_clock>;

inline const TimePoint& GetStartTime() const { return fStartTime; }
[[nodiscard]] inline const TimePoint& GetStartTime() const { return fStartTime; }
inline void SetStartTime(TimePoint t) { fStartTime = t; }

private:
Expand Down
4 changes: 2 additions & 2 deletions include/RMGTrackingAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class RMGTrackingAction : public G4UserTrackingAction {
RMGTrackingAction(RMGTrackingAction&&) = delete;
RMGTrackingAction& operator=(RMGTrackingAction&&) = delete;

virtual void PreUserTrackingAction(const G4Track*) override;
virtual void PostUserTrackingAction(const G4Track*) override;
void PreUserTrackingAction(const G4Track*) override;
void PostUserTrackingAction(const G4Track*) override;
inline G4TrackingManager* GetTrackingManager() {
return G4UserTrackingAction::fpTrackingManager;
};
Expand Down
4 changes: 2 additions & 2 deletions include/RMGVVertexGenerator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class RMGVVertexGenerator {
return false;
}
inline void SetMaxAttempts(int val) { fMaxAttempts = val; }
inline int GetMaxAttempts() { return fMaxAttempts; }
[[nodiscard]] inline int GetMaxAttempts() const { return fMaxAttempts; }

#if RMG_HAS_BXDECAY0
inline void ShootVertex(G4ThreeVector& v) { GenerateVertex(v); }
inline void ShootVertex(G4ThreeVector& v) override { GenerateVertex(v); }
#endif

protected:
Expand Down
12 changes: 6 additions & 6 deletions include/RMGVertexConfinement.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RMGVertexConfinement : public RMGVVertexGenerator {
public:

struct GenericGeometricalSolidData {
std::string g4_name = "";
std::string g4_name;
G4ThreeVector volume_center = G4ThreeVector(0, 0, 0);
double sphere_inner_radius = 0;
double sphere_outer_radius = -1;
Expand Down Expand Up @@ -98,19 +98,19 @@ class RMGVertexConfinement : public RMGVVertexGenerator {
SampleableObjectCollection() = default;
inline ~SampleableObjectCollection() { data.clear(); }

const SampleableObject& SurfaceWeightedRand() const;
const SampleableObject& VolumeWeightedRand() const;
bool IsInside(const G4ThreeVector& point) const;
[[nodiscard]] const SampleableObject& SurfaceWeightedRand() const;
[[nodiscard]] const SampleableObject& VolumeWeightedRand() const;
[[nodiscard]] bool IsInside(const G4ThreeVector& vertex) const;

// emulate std::vector
size_t size() const { return data.size(); }
[[nodiscard]] size_t size() const { return data.size(); }
SampleableObject& at(size_t i) { return data.at(i); }
void emplace_back(G4VPhysicalVolume* v, const G4RotationMatrix& r, const G4ThreeVector& t,
G4VSolid* s);
inline void push_back(const SampleableObject& obj) {
this->emplace_back(obj.physical_volume, obj.rotation, obj.translation, obj.sampling_solid);
}
inline bool empty() const { return data.empty(); }
[[nodiscard]] inline bool empty() const { return data.empty(); }
inline SampleableObject& back() { return data.back(); }
inline void clear() { data.clear(); }
inline void insert(SampleableObjectCollection& other) {
Expand Down
1 change: 0 additions & 1 deletion include/RMGVertexFromFile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "G4GenericMessenger.hh"
#include "G4ThreeVector.hh"

class G4Event;
class G4VAnalysisReader;
class RMGVertexFromFile : public RMGVVertexGenerator {

Expand Down
13 changes: 5 additions & 8 deletions src/RMGGeneratorUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#endif

bool RMGGeneratorUtil::IsSampleable(std::string g4_solid_type) {
if (g4_solid_type == "G4Box" or g4_solid_type == "G4Orb" or g4_solid_type == "G4Sphere" or
g4_solid_type == "G4Tubs")
return true;
else return false;
return g4_solid_type == "G4Box" or g4_solid_type == "G4Orb" or g4_solid_type == "G4Sphere" or
g4_solid_type == "G4Tubs";
}

G4ThreeVector RMGGeneratorUtil::rand(const G4VSolid* vol, bool on_surface) {
Expand All @@ -45,7 +43,7 @@ G4ThreeVector RMGGeneratorUtil::rand(const G4VSolid* vol, bool on_surface) {
return RMGGeneratorUtil::rand(dynamic_cast<const G4Tubs*>(vol), on_surface);
else {
RMGLog::OutDev(RMGLog::fatal, "'", entity, "' is not supported (implement me)");
return G4ThreeVector();
return {};
}
}

Expand Down Expand Up @@ -78,10 +76,9 @@ G4ThreeVector RMGGeneratorUtil::rand(const G4Box* box, bool on_surface) {
y = dy * (2 * _g4rand() - 1);
z = dz * (2 * _g4rand() - 1);
}
return G4ThreeVector(x, y, z);
return {x, y, z};
} else {
return G4ThreeVector(dx * (2 * _g4rand() - 1), dy * (2 * _g4rand() - 1),
dz * (2 * _g4rand() - 1));
return {dx * (2 * _g4rand() - 1), dy * (2 * _g4rand() - 1), dz * (2 * _g4rand() - 1)};
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/RMGGermaniumDetector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ G4ThreadLocal G4Allocator<RMGGermaniumDetectorHit>* RMGGermaniumDetectorHitAlloc

// NOTE: does this make sense?
G4bool RMGGermaniumDetectorHit::operator==(const RMGGermaniumDetectorHit& right) const {
return (this == &right) ? true : false;
return this == &right;
}

void RMGGermaniumDetectorHit::Print() {
Expand Down Expand Up @@ -131,7 +131,7 @@ bool RMGGermaniumDetector::ProcessHits(G4Step* step, G4TouchableHistory* /*histo
return true;
}

void RMGGermaniumDetector::EndOfEvent(G4HCofThisEvent* /*hit_coll*/) { return; }
void RMGGermaniumDetector::EndOfEvent(G4HCofThisEvent* /*hit_coll*/) {}

void RMGGermaniumDetector::DefineCommands() {

Expand Down
2 changes: 1 addition & 1 deletion src/RMGHardware.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void RMGHardware::ConstructSDandField() {
fActiveDetectorsInitialized = true;
}

std::string vec_repr = "";
std::string vec_repr;
for (const auto& d : fActiveDetectors) vec_repr += std::string(magic_enum::enum_name(d)) + ", ";
if (vec_repr.size() > 2) vec_repr.erase(vec_repr.size() - 2);
RMGLog::OutFormat(RMGLog::debug, "List of activated detectors: [{}]", vec_repr);
Expand Down
14 changes: 8 additions & 6 deletions src/RMGLog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ RMGLog::RMGLog() {

void RMGLog::StartupInfo() {

std::string message = "";
message += " _ __ ___ _ __ ___ __ _ __ _ ___ \n";
message += " | '__/ _ \\ '_ ` _ \\ / _` |/ _` |/ _ \\\n";
message += " | | | __/ | | | | | (_| | (_| | __/\n";
message += " |_| \\___|_| |_| |_|\\__,_|\\__, |\\___| v" + RMGLog::fVersion + "\n";
message += " |___/ \n";
std::string message;
// clang-format off
message += R"( _ __ ___ _ __ ___ __ _ __ _ ___ )" "\n";
message += R"( | '__/ _ \ '_ ` _ \ / _` |/ _` |/ _ \)" "\n";
message += R"( | | | __/ | | | | | (_| | (_| | __/)" "\n";
message += R"( |_| \___|_| |_| |_|\__,_|\__, |\___| v)" + RMGLog::fVersion + "\n";
message += R"( |___/ )" "\n";
// clang-format on

// write message to screen
if (RMGLog::fMinimumLogLevel <= RMGLog::summary) G4cout << message << G4endl;
Expand Down
2 changes: 1 addition & 1 deletion src/RMGManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void RMGManager::Initialize() {
if (!fG4VisManager) this->SetUpDefaultG4VisManager();
fG4VisManager->Initialize();

std::string _str = "";
std::string _str;
for (const auto& i : fG4VisManager->GetAvailableGraphicsSystems()) {
_str += i->GetNickname() + " ";
}
Expand Down
4 changes: 1 addition & 3 deletions src/RMGMasterGenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@

#include "RMGTools.hh"

RMGMasterGenerator::RMGMasterGenerator()
: fConfinement(RMGMasterGenerator::Confinement::kUnConfined), fVertexGeneratorObj(nullptr),
fGenerator(RMGMasterGenerator::Generator::kUndefined), fGeneratorObj(nullptr) {
RMGMasterGenerator::RMGMasterGenerator() : fVertexGeneratorObj(nullptr), fGeneratorObj(nullptr) {

this->DefineCommands();
}
Expand Down
Loading

0 comments on commit f0df7ca

Please sign in to comment.