From cd97cf2cc09573053526f02f4437e4fbca2e78d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= <26327373+vkucera@users.noreply.github.com> Date: Wed, 21 Oct 2026 18:24:12 +0200 Subject: [PATCH] Fix casts Fixes C-style, redundant, wrong, and dangerous casts (found by Clang-Tidy and cpplint). - Remove redundant casts. (`readability-redundant-casting`) - Remove `const_cast`. (`cppcoreguidelines-pro-type-const-cast`) - Replace C-style casts with `static_cast`. (`google-readability-casting`, cpplint: `readability/casting`) - Replace unsafe C-style casts with `dynamic_cast`. (`cppcoreguidelines-pro-type-cstyle-cast`) - Replace `static_cast` in downcasts with `dynamic_cast`. (`cppcoreguidelines-pro-type-static-cast-downcast`) - Replace `reinterpret_cast` with `static_cast` or `dynamic_cast`. (`cppcoreguidelines-pro-type-reinterpret-cast`) - Replace two-step casting through `void*` with `reinterpret_cast`. (`bugprone-casting-through-void`) --- ALICE3/Core/FlatLutEntry.cxx | 16 ++++++++-------- ALICE3/Core/FlatLutWriter.cxx | 4 ++-- ALICE3/Core/FlatTrackSmearer.cxx | 2 +- ALICE3/TableProducer/OTF/onTheFlyMidPid.cxx | 2 +- ALICE3/TableProducer/alice3-correlatorDDbar.cxx | 2 +- ALICE3/TableProducer/alice3DqTableMaker.cxx | 4 ++-- ALICE3/TableProducer/alice3HfSelector3Prong.cxx | 2 +- .../TableProducer/alice3TrackingTranslator.cxx | 2 +- ALICE3/Tasks/alice3-qa-multiplicity.cxx | 2 +- ALICE3/Tasks/alice3DqEfficiency.cxx | 2 +- ALICE3/Tasks/alice3HfTask3Prong.cxx | 2 +- 11 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ALICE3/Core/FlatLutEntry.cxx b/ALICE3/Core/FlatLutEntry.cxx index 984157f4520..66a6cedf860 100644 --- a/ALICE3/Core/FlatLutEntry.cxx +++ b/ALICE3/Core/FlatLutEntry.cxx @@ -140,23 +140,23 @@ size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt const lutEntry_t* FlatLutData::getEntryRef(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const { size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin); - return reinterpret_cast(mDataRef.data() + offset); + return reinterpret_cast(mDataRef.data() + offset); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin) { size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin); - return reinterpret_cast(mData.data() + offset); + return reinterpret_cast(mData.data() + offset); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } const lutHeader_t& FlatLutData::getHeaderRef() const { - return *static_cast(static_cast(mDataRef.data())); + return *reinterpret_cast(mDataRef.data()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } lutHeader_t& FlatLutData::getHeader() { - return *static_cast(static_cast(mData.data())); + return *reinterpret_cast(mData.data()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } void FlatLutData::updateRef() @@ -216,7 +216,7 @@ lutHeader_t FlatLutData::previewHeader(const uint8_t* buffer, size_t size) if (size < sizeof(lutHeader_t)) { throw framework::runtime_error_f("Buffer too small for LUT header: expected at least %zu, got %zu", sizeof(lutHeader_t), size); } - const auto* header = static_cast(static_cast(buffer)); + const auto* header = reinterpret_cast(buffer); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) if (!header->checkVersion()) { throw framework::runtime_error_f("LUT header version mismatch: expected %d, got %d", LUTCOVM_VERSION, header->version); } @@ -246,7 +246,7 @@ FlatLutData FlatLutData::ViewFromBuffer(const uint8_t* buffer, size_t size) FlatLutData FlatLutData::ViewFromBuffer(std::span const& span) { - return ViewFromBuffer(reinterpret_cast(span.data()), span.size_bytes()); + return ViewFromBuffer(reinterpret_cast(span.data()), span.size_bytes()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } bool FlatLutData::isLoaded() const @@ -257,7 +257,7 @@ bool FlatLutData::isLoaded() const lutHeader_t FlatLutData::previewHeader(std::ifstream& file, const char* filename) { lutHeader_t tempHeader; - file.read(reinterpret_cast(&tempHeader), sizeof(lutHeader_t)); + file.read(reinterpret_cast(&tempHeader), sizeof(lutHeader_t)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) if (file.gcount() != static_cast(sizeof(lutHeader_t))) { throw framework::runtime_error_f("Failed to read LUT header from %s", filename); } @@ -282,7 +282,7 @@ FlatLutData FlatLutData::loadFromFile(std::ifstream& file, const char* filename) size_t numEntries = static_cast(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins; size_t entriesSize = numEntries * sizeof(lutEntry_t); - file.read(reinterpret_cast(data.data() + headerSize), entriesSize); + file.read(reinterpret_cast(data.data() + headerSize), entriesSize); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) if (file.gcount() != static_cast(entriesSize)) { throw framework::runtime_error_f("Failed to read LUT entries from %s: expected %zu bytes, got %zu", filename, entriesSize, static_cast(file.gcount())); } diff --git a/ALICE3/Core/FlatLutWriter.cxx b/ALICE3/Core/FlatLutWriter.cxx index dfb874af223..a85c242a8b1 100644 --- a/ALICE3/Core/FlatLutWriter.cxx +++ b/ALICE3/Core/FlatLutWriter.cxx @@ -266,7 +266,7 @@ void FlatLutWriter::lutWrite(const char* filename, int pdg, float field, size_t setMap(lutHeader.ptmap, mPtBinning); // Write header to file - lutFile.write(reinterpret_cast(&lutHeader), sizeof(lutHeader_t)); + lutFile.write(reinterpret_cast(&lutHeader), sizeof(lutHeader_t)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) if (!lutFile.good()) { LOGF(error, "Failed to write LUT header to %s", filename); lutFile.close(); @@ -361,7 +361,7 @@ void FlatLutWriter::lutWrite(const char* filename, int pdg, float field, size_t diagonalise(lutEntry); // Write entry to file - lutFile.write(reinterpret_cast(&lutEntry), sizeof(lutEntry_t)); + lutFile.write(reinterpret_cast(&lutEntry), sizeof(lutEntry_t)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) if (!lutFile.good()) { LOGF(error, "Failed to write LUT entry at index [%d,%d,%d,%d]", inch, irad, ieta, ipt); lutFile.close(); diff --git a/ALICE3/Core/FlatTrackSmearer.cxx b/ALICE3/Core/FlatTrackSmearer.cxx index 1bd79f088a8..9617315d2e4 100644 --- a/ALICE3/Core/FlatTrackSmearer.cxx +++ b/ALICE3/Core/FlatTrackSmearer.cxx @@ -171,7 +171,7 @@ bool TrackSmearer::viewTable(int pdg, const uint8_t* buffer, size_t size, bool f bool TrackSmearer::viewTable(int pdg, std::span const& span, bool forceReload) { - return viewTable(pdg, reinterpret_cast(span.data()), span.size_bytes(), forceReload); + return viewTable(pdg, reinterpret_cast(span.data()), span.size_bytes(), forceReload); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) } bool TrackSmearer::hasTable(int pdg) const diff --git a/ALICE3/TableProducer/OTF/onTheFlyMidPid.cxx b/ALICE3/TableProducer/OTF/onTheFlyMidPid.cxx index 6b69d082961..689da6f6e83 100644 --- a/ALICE3/TableProducer/OTF/onTheFlyMidPid.cxx +++ b/ALICE3/TableProducer/OTF/onTheFlyMidPid.cxx @@ -1036,7 +1036,7 @@ struct OnTheFlyMidPid { if (etStatus) { histos.fill(HIST("h1dEventTimedelta"), eventCollisionTimePS - tzero[0]); } - static_cast(listEfficiency->At(0))->Fill(etStatus, dNdEta); + dynamic_cast(listEfficiency->At(0))->Fill(etStatus, dNdEta); } // Then we do a second loop to compute the measured quantities with the measured event time diff --git a/ALICE3/TableProducer/alice3-correlatorDDbar.cxx b/ALICE3/TableProducer/alice3-correlatorDDbar.cxx index 0e27bec4476..5603fb97348 100644 --- a/ALICE3/TableProducer/alice3-correlatorDDbar.cxx +++ b/ALICE3/TableProducer/alice3-correlatorDDbar.cxx @@ -115,7 +115,7 @@ struct alice3correlatorddbar { void init(InitContext&) { - auto vbins = (std::vector)binsPt; + auto vbins = std::vector(binsPt); registry.add("hMass", "D0,D0bar candidates;inv. mass (#pi K) (GeV/#it{c}^{2});entries", {HistType::kTH2F, {{massAxisBins, massAxisMin, massAxisMax}, {vbins, "#it{p}_{T} (GeV/#it{c})"}}}); registry.add("hMassD0", "D0,D0bar candidates;inv. mass D0 only (#pi K) (GeV/#it{c}^{2});entries", {HistType::kTH2F, {{massAxisBins, massAxisMin, massAxisMax}, {vbins, "#it{p}_{T} (GeV/#it{c})"}}}); registry.add("hMassD0bar", "D0,D0bar candidates;inv. mass D0bar only (#pi K) (GeV/#it{c}^{2});entries", {HistType::kTH2F, {{massAxisBins, massAxisMin, massAxisMax}, {vbins, "#it{p}_{T} (GeV/#it{c})"}}}); diff --git a/ALICE3/TableProducer/alice3DqTableMaker.cxx b/ALICE3/TableProducer/alice3DqTableMaker.cxx index 041fc71ebfe..1cbbb4fa8a9 100644 --- a/ALICE3/TableProducer/alice3DqTableMaker.cxx +++ b/ALICE3/TableProducer/alice3DqTableMaker.cxx @@ -301,7 +301,7 @@ struct Alice3DqTableMaker { fStatsList.setObject(new TList()); fStatsList->SetOwner(true); std::vector eventLabels{"Collisions before filtering", "Before cuts", "After cuts"}; - TH2I* histEvents = new TH2I("EventStats", "Event statistics", eventLabels.size(), -0.5, eventLabels.size() - 0.5, o2::aod::evsel::kNsel + 1, -0.5, (float)o2::aod::evsel::kNsel + 0.5); + TH2I* histEvents = new TH2I("EventStats", "Event statistics", eventLabels.size(), -0.5, eventLabels.size() - 0.5, o2::aod::evsel::kNsel + 1, -0.5, static_cast(o2::aod::evsel::kNsel) + 0.5); int ibX = 1; for (auto label = eventLabels.begin(); label != eventLabels.end(); label++, ibX++) { histEvents->GetXaxis()->SetBinLabel(ibX, (*label).Data()); @@ -583,7 +583,7 @@ struct Alice3DqTableMaker { if (fDoDetailedQA) { j = 0; for (const auto& cut : fTrackCuts) { - if ((trackTempFilterMap & (uint8_t(1) << j)) != 0u) { + if ((trackTempFilterMap & (static_cast(1) << j)) != 0u) { fHistMan->FillHistClass(Form("TrackBarrel_%s_%s", cut->GetName(), sig->GetName()), dqefficiency_helpers::varValues()); // fill the reconstructed truth } j++; diff --git a/ALICE3/TableProducer/alice3HfSelector3Prong.cxx b/ALICE3/TableProducer/alice3HfSelector3Prong.cxx index 89c34ff1cf3..3669ffd834d 100644 --- a/ALICE3/TableProducer/alice3HfSelector3Prong.cxx +++ b/ALICE3/TableProducer/alice3HfSelector3Prong.cxx @@ -114,7 +114,7 @@ struct Alice3HfSelector3Prong { labels[1 + aod::SelectionStep::RecoPID] = "Skims & Topological & PID selections"; labels[1 + aod::SelectionStep::RecoMl] = "ML selection"; static const AxisSpec axisSelections = {kNBinsSelections, 0.5, kNBinsSelections + 0.5, ""}; - registry.add("hSelections", "Selections;;#it{p}_{T} (GeV/#it{c})", {HistType::kTH2F, {axisSelections, {(std::vector)binsPt, "#it{p}_{T} (GeV/#it{c})"}}}); + registry.add("hSelections", "Selections;;#it{p}_{T} (GeV/#it{c})", {HistType::kTH2F, {axisSelections, {std::vector(binsPt), "#it{p}_{T} (GeV/#it{c})"}}}); for (int iBin = 0; iBin < kNBinsSelections; ++iBin) { registry.get(HIST("hSelections"))->GetXaxis()->SetBinLabel(iBin + 1, labels[iBin].data()); } diff --git a/ALICE3/TableProducer/alice3TrackingTranslator.cxx b/ALICE3/TableProducer/alice3TrackingTranslator.cxx index 26f53cf9f5f..e0e742fc9d0 100644 --- a/ALICE3/TableProducer/alice3TrackingTranslator.cxx +++ b/ALICE3/TableProducer/alice3TrackingTranslator.cxx @@ -360,7 +360,7 @@ struct Alice3TrackingTranslator { if (filesList) { TIter next(filesList); TSystemFile* file; - while ((file = static_cast(next()))) { + while ((file = dynamic_cast(next()))) { TString fname = file->GetName(); LOG(debug) << "Found file: " << fname.Data(); if (!file->IsDirectory() && fname.EndsWith(".root")) { diff --git a/ALICE3/Tasks/alice3-qa-multiplicity.cxx b/ALICE3/Tasks/alice3-qa-multiplicity.cxx index a5440ff8d53..5e81cd8fd5a 100644 --- a/ALICE3/Tasks/alice3-qa-multiplicity.cxx +++ b/ALICE3/Tasks/alice3-qa-multiplicity.cxx @@ -41,7 +41,7 @@ struct ALICE3MultTask { void init(InitContext&) { - const AxisSpec axisMult{MaxMult.value > 10000.f ? 10000 : (int)MaxMult, 0, MaxMult, "Reconstructed tracks"}; + const AxisSpec axisMult{MaxMult.value > 10000.f ? 10000 : static_cast(MaxMult), 0, MaxMult, "Reconstructed tracks"}; TString tit = Form("%.3f < #it{#eta} < %.3f", MinEta.value, MaxEta.value); histos.add("multiplicity/numberOfTracksNoCut", tit, kTH1D, {axisMult}); histos.add("multiplicity/numberOfTracks", tit, kTH1D, {axisMult}); diff --git a/ALICE3/Tasks/alice3DqEfficiency.cxx b/ALICE3/Tasks/alice3DqEfficiency.cxx index 29361efe44e..d90a6f0b320 100644 --- a/ALICE3/Tasks/alice3DqEfficiency.cxx +++ b/ALICE3/Tasks/alice3DqEfficiency.cxx @@ -983,7 +983,7 @@ struct Alice3DqEfficiencyAnalysisSameEventPairing { fHistMan->SetUseDefaultVariableNames(true); fHistMan->SetDefaultVarNames(dqefficiency_helpers::varNames(), dqefficiency_helpers::varUnits()); - VarManager::SetCollisionSystem((TString)fConfigOptions.syst, fConfigOptions.energy); // set collision system and center of mass energy + VarManager::SetCollisionSystem(TString(fConfigOptions.syst), fConfigOptions.energy); // set collision system and center of mass energy defineHistograms(fHistMan, histNames.Data(), cfgAddSEPHistogram.value.data()); // define all histograms dqhistograms::AddHistogramsFromJSON(fHistMan, cfgAddJSONHistograms.value.c_str()); // ad-hoc histograms via JSON diff --git a/ALICE3/Tasks/alice3HfTask3Prong.cxx b/ALICE3/Tasks/alice3HfTask3Prong.cxx index 4f21bada994..823d054395a 100644 --- a/ALICE3/Tasks/alice3HfTask3Prong.cxx +++ b/ALICE3/Tasks/alice3HfTask3Prong.cxx @@ -119,7 +119,7 @@ struct Alice3HfTask3Prong { registry.add(("MC/gen/nonprompt/" + histoName + "GenNonPrompt").c_str(), ("MC particles (matched, non-prompt);" + xAxisTitle + ";" + yAxisTitle).c_str(), configSpec); }; - auto vbins = (std::vector)binsPt; + auto vbins = std::vector(binsPt); /// Reconstructed Histograms addHistogramsRec("hMass", "inv. mass (p K #pi) (GeV/#it{c}^{2})", "", {HistType::kTH1F, {{600, 1.98, 2.58}}});