[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
carlosgalvezp wrote: For future reference, `modernize` is typically related to new features of new C++ standards, so this check wouldn't quite fit there. Great that you found an existing check that suits your needs! https://github.com/llvm/llvm-project/pull/118033 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
https://github.com/hjanuschka closed https://github.com/llvm/llvm-project/pull/118033 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
hjanuschka wrote: ohhh TIL! lets close this PR `readability-redundant-casting` with: `IgnoreTypeAliases` does what i was chasing! /close https://github.com/llvm/llvm-project/pull/118033 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
firewave wrote: How is this different from `readability-redundant-casting`? https://github.com/llvm/llvm-project/pull/118033 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 866755f8da588ec2efbcac60679b9d9f5c4636a9 5ee115e7cd83fe5a59c0284e91893b71df79be0c --extensions cpp,h -- clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h clang-tools-extra/test/clang-tidy/checkers/modernize/modernize-cleanup-static-cast.cpp clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp `` View the diff from clang-format here. ``diff diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp index 1fdb0cff14..f7cb40e32d 100644 --- a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp @@ -18,7 +18,6 @@ using namespace clang::ast_matchers; namespace clang::tidy::modernize { - std::string getText(const clang::Expr *E, const clang::ASTContext &Context) { auto &SM = Context.getSourceManager(); auto Range = clang::CharSourceRange::getTokenRange(E->getSourceRange()); @@ -28,11 +27,10 @@ std::string getText(const clang::Expr *E, const clang::ASTContext &Context) { void CleanupStaticCastCheck::registerMatchers(MatchFinder *Finder) { // Match static_cast expressions not in templates Finder->addMatcher( - cxxStaticCastExpr( - unless(hasAncestor(functionTemplateDecl())), - unless(hasAncestor(classTemplateDecl())), - unless(isInTemplateInstantiation())) - .bind("cast"), + cxxStaticCastExpr(unless(hasAncestor(functionTemplateDecl())), +unless(hasAncestor(classTemplateDecl())), +unless(isInTemplateInstantiation())) + .bind("cast"), this); } @@ -52,18 +50,18 @@ void CleanupStaticCastCheck::check(const MatchFinder::MatchResult &Result) { // Compare canonical types and qualifiers SourceType = SourceType.getCanonicalType(); TargetType = TargetType.getCanonicalType(); - + if (SourceType == TargetType) { -auto Diag = +auto Diag = diag(Cast->getBeginLoc(), - "redundant static_cast to the same type %0") // Removed single quotes + "redundant static_cast to the same type %0") // Removed single + // quotes << TargetType; std::string ReplacementText = getText(SubExpr, *Result.Context); - -Diag << FixItHint::CreateReplacement( -Cast->getSourceRange(), -ReplacementText); + +Diag << FixItHint::CreateReplacement(Cast->getSourceRange(), + ReplacementText); } } diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h index a96e7cac3f..3c719a7376 100644 --- a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h @@ -23,8 +23,8 @@ namespace clang::tidy::modernize { /// foo(s); /// \endcode /// -/// Note: This check intentionally ignores redundant casts in template instantiations -/// as they might be needed for other template parameter types. +/// Note: This check intentionally ignores redundant casts in template +/// instantiations as they might be needed for other template parameter types. class CleanupStaticCastCheck : public ClangTidyCheck { public: CleanupStaticCastCheck(StringRef Name, ClangTidyContext *Context) diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 364f1d6b20..edb11d8afe 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModuleRegistry.h" #include "AvoidBindCheck.h" #include "AvoidCArraysCheck.h" +#include "CleanupStaticCastCheck.h" #include "ConcatNestedNamespacesCheck.h" #include "DeprecatedHeadersCheck.h" #include "DeprecatedIosBaseAliasesCheck.h" @@ -42,7 +43,6 @@ #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" #include "UseRangesCheck.h" -#include "CleanupStaticCastCheck.h" #include "UseStartsEndsWithCheck.h" #include "UseStdFormatCheck.h" #include "UseStdNumbersCheck.h" @@ -124,7 +124,7 @@ public: "modernize-use-uncaught-exceptions"); CheckFactories.registerCheck("modernize-use-using"); CheckFactories.registerCheck( - "modernize-cleanup-static-cast"); +"modernize-cleanup-static-cast"); } }; `` https://github.com/llvm/llvm-project/pull/118033 ___ cfe-commits mailing li
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
llvmbot wrote: @llvm/pr-subscribers-clang-tools-extra Author: Helmut Januschka (hjanuschka) Changes > Add a new check that detects and removes redundant static_cast operations where the source and target types are identical. This helps clean up code after type system changes, improving readability and reducing opportunities for errors during future refactoring. before polishing it and adding documentation, can someone tell me if the check makes sense to be picked in modernize? --- Full diff: https://github.com/llvm/llvm-project/pull/118033.diff 5 Files Affected: - (modified) clang-tools-extra/clang-tidy/modernize/CMakeLists.txt (+1) - (added) clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp (+70) - (added) clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h (+38) - (modified) clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp (+3) - (added) clang-tools-extra/test/clang-tidy/checkers/modernize/modernize-cleanup-static-cast.cpp (+42) ``diff diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index c919d49b42873a..9ce32473c201bc 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -49,6 +49,7 @@ add_clang_library(clangTidyModernizeModule STATIC UseTransparentFunctorsCheck.cpp UseUncaughtExceptionsCheck.cpp UseUsingCheck.cpp + CleanupStaticCastCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp new file mode 100644 index 00..1fdb0cff149883 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp @@ -0,0 +1,70 @@ +//===--- CleanupStaticCastCheck.cpp - clang-tidy-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "CleanupStaticCastCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + + +std::string getText(const clang::Expr *E, const clang::ASTContext &Context) { + auto &SM = Context.getSourceManager(); + auto Range = clang::CharSourceRange::getTokenRange(E->getSourceRange()); + return clang::Lexer::getSourceText(Range, SM, Context.getLangOpts()).str(); +} + +void CleanupStaticCastCheck::registerMatchers(MatchFinder *Finder) { + // Match static_cast expressions not in templates + Finder->addMatcher( + cxxStaticCastExpr( + unless(hasAncestor(functionTemplateDecl())), + unless(hasAncestor(classTemplateDecl())), + unless(isInTemplateInstantiation())) + .bind("cast"), + this); +} + +void CleanupStaticCastCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Cast = Result.Nodes.getNodeAs("cast"); + if (!Cast) +return; + + const Expr *SubExpr = Cast->getSubExpr()->IgnoreParenImpCasts(); + QualType SourceType = SubExpr->getType(); + QualType TargetType = Cast->getType(); + + // Skip if either type is dependent + if (SourceType->isDependentType() || TargetType->isDependentType()) +return; + + // Compare canonical types and qualifiers + SourceType = SourceType.getCanonicalType(); + TargetType = TargetType.getCanonicalType(); + + if (SourceType == TargetType) { +auto Diag = +diag(Cast->getBeginLoc(), + "redundant static_cast to the same type %0") // Removed single quotes +<< TargetType; + +std::string ReplacementText = getText(SubExpr, *Result.Context); + +Diag << FixItHint::CreateReplacement( +Cast->getSourceRange(), +ReplacementText); + } +} + +} // namespace clang::tidy::modernize \ No newline at end of file diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h new file mode 100644 index 00..a96e7cac3f1f72 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h @@ -0,0 +1,38 @@ +// CleanupStaticCastCheck.h +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_CLEANUPSTATICCASTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_CLEANUPSTATICCASTCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::modernize { + +/// Finds and removes static_cast where target type exactly matches source type. +/// +/// This check helps clean up redundant static_cast operations that remain af
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
llvmbot wrote: @llvm/pr-subscribers-clang-tidy Author: Helmut Januschka (hjanuschka) Changes > Add a new check that detects and removes redundant static_cast operations where the source and target types are identical. This helps clean up code after type system changes, improving readability and reducing opportunities for errors during future refactoring. before polishing it and adding documentation, can someone tell me if the check makes sense to be picked in modernize? --- Full diff: https://github.com/llvm/llvm-project/pull/118033.diff 5 Files Affected: - (modified) clang-tools-extra/clang-tidy/modernize/CMakeLists.txt (+1) - (added) clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp (+70) - (added) clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h (+38) - (modified) clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp (+3) - (added) clang-tools-extra/test/clang-tidy/checkers/modernize/modernize-cleanup-static-cast.cpp (+42) ``diff diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index c919d49b42873a..9ce32473c201bc 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -49,6 +49,7 @@ add_clang_library(clangTidyModernizeModule STATIC UseTransparentFunctorsCheck.cpp UseUncaughtExceptionsCheck.cpp UseUsingCheck.cpp + CleanupStaticCastCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp new file mode 100644 index 00..1fdb0cff149883 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp @@ -0,0 +1,70 @@ +//===--- CleanupStaticCastCheck.cpp - clang-tidy-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "CleanupStaticCastCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + + +std::string getText(const clang::Expr *E, const clang::ASTContext &Context) { + auto &SM = Context.getSourceManager(); + auto Range = clang::CharSourceRange::getTokenRange(E->getSourceRange()); + return clang::Lexer::getSourceText(Range, SM, Context.getLangOpts()).str(); +} + +void CleanupStaticCastCheck::registerMatchers(MatchFinder *Finder) { + // Match static_cast expressions not in templates + Finder->addMatcher( + cxxStaticCastExpr( + unless(hasAncestor(functionTemplateDecl())), + unless(hasAncestor(classTemplateDecl())), + unless(isInTemplateInstantiation())) + .bind("cast"), + this); +} + +void CleanupStaticCastCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Cast = Result.Nodes.getNodeAs("cast"); + if (!Cast) +return; + + const Expr *SubExpr = Cast->getSubExpr()->IgnoreParenImpCasts(); + QualType SourceType = SubExpr->getType(); + QualType TargetType = Cast->getType(); + + // Skip if either type is dependent + if (SourceType->isDependentType() || TargetType->isDependentType()) +return; + + // Compare canonical types and qualifiers + SourceType = SourceType.getCanonicalType(); + TargetType = TargetType.getCanonicalType(); + + if (SourceType == TargetType) { +auto Diag = +diag(Cast->getBeginLoc(), + "redundant static_cast to the same type %0") // Removed single quotes +<< TargetType; + +std::string ReplacementText = getText(SubExpr, *Result.Context); + +Diag << FixItHint::CreateReplacement( +Cast->getSourceRange(), +ReplacementText); + } +} + +} // namespace clang::tidy::modernize \ No newline at end of file diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h new file mode 100644 index 00..a96e7cac3f1f72 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h @@ -0,0 +1,38 @@ +// CleanupStaticCastCheck.h +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_CLEANUPSTATICCASTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_CLEANUPSTATICCASTCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::modernize { + +/// Finds and removes static_cast where target type exactly matches source type. +/// +/// This check helps clean up redundant static_cast operations that remain after +//
[clang-tools-extra] [clang-tidy] Add modernize-cleanup-static-cast check (PR #118033)
https://github.com/hjanuschka created https://github.com/llvm/llvm-project/pull/118033 > Add a new check that detects and removes redundant static_cast operations > where the source and target types are identical. This helps clean up code > after type system changes, improving readability and reducing opportunities > for errors during future refactoring. before polishing it and adding documentation, can someone tell me if the check makes sense to be picked in modernize? >From 062aebbdc5bb858cdc25c4995752b9c8a0eb34e8 Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Thu, 28 Nov 2024 21:15:25 +0100 Subject: [PATCH 1/2] [clang-tidy] Add modernize-cleanup-static-cast check Add a new check that detects and removes redundant static_cast operations where the source and target types are identical. This helps clean up code after type system changes, improving readability and reducing opportunities for errors during future refactoring. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/CleanupStaticCastCheck.cpp | 74 +++ .../modernize/CleanupStaticCastCheck.h| 38 ++ .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize-cleanup-static-cast.cpp | 42 +++ 5 files changed, 158 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.h create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/modernize-cleanup-static-cast.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index c919d49b42873a..9ce32473c201bc 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -49,6 +49,7 @@ add_clang_library(clangTidyModernizeModule STATIC UseTransparentFunctorsCheck.cpp UseUncaughtExceptionsCheck.cpp UseUsingCheck.cpp + CleanupStaticCastCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp new file mode 100644 index 00..545ce4c5a58357 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/CleanupStaticCastCheck.cpp @@ -0,0 +1,74 @@ +//===--- CleanupStaticCastCheck.cpp - clang-tidy-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "CleanupStaticCastCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace { +std::string getText(const clang::Expr *E, const clang::ASTContext &Context) { + auto &SM = Context.getSourceManager(); + auto Range = clang::CharSourceRange::getTokenRange(E->getSourceRange()); + return clang::Lexer::getSourceText(Range, SM, Context.getLangOpts()).str(); +} +} // namespace + +namespace clang::tidy::modernize { + +void CleanupStaticCastCheck::registerMatchers(MatchFinder *Finder) { + // Match static_cast expressions not in templates + Finder->addMatcher( + cxxStaticCastExpr( + unless(hasAncestor(functionTemplateDecl())), + unless(hasAncestor(classTemplateDecl())), + unless(isInTemplateInstantiation())) + .bind("cast"), + this); +} + +void CleanupStaticCastCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Cast = Result.Nodes.getNodeAs("cast"); + if (!Cast) +return; + + // Get the source expression and its type + const Expr *SubExpr = Cast->getSubExpr()->IgnoreParenImpCasts(); + QualType SourceType = SubExpr->getType(); + QualType TargetType = Cast->getType(); + + // Skip if either type is dependent + if (SourceType->isDependentType() || TargetType->isDependentType()) +return; + + // Compare canonical types and qualifiers + SourceType = SourceType.getCanonicalType(); + TargetType = TargetType.getCanonicalType(); + + if (SourceType == TargetType) { +auto Diag = +diag(Cast->getBeginLoc(), + "redundant static_cast to the same type %0") // Removed single quotes +<< TargetType; + +// Use our helper function to get the source text +std::string ReplacementText = getText(SubExpr, *Result.Context); + +// Suggest removing the cast +Diag << FixItHint::CreateReplacement( +Cast->getSourceRange(), +ReplacementText); + } +} + +} // namespace clang::tidy::modernize \ No newline at end of file diff --git a/cla