diff options
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp | 43 | ||||
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/misc-sizeof-container.cpp | 5 |
2 files changed, 3 insertions, 45 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp b/clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp index ffb46dfcf9c..5bd9f75c815 100644 --- a/clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp @@ -16,21 +16,6 @@ using namespace clang::ast_matchers; namespace clang { namespace tidy { -namespace { - -bool needsParens(const Expr *E) { - E = E->IgnoreImpCasts(); - if (isa<BinaryOperator>(E) || isa<ConditionalOperator>(E)) - return true; - if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(E)) { - return Op->getNumArgs() == 2 && Op->getOperator() != OO_Call && - Op->getOperator() != OO_Subscript; - } - return false; -} - -} // anonymous namespace - void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( expr(unless(isInTemplateInstantiation()), @@ -52,31 +37,9 @@ void SizeofContainerCheck::check(const MatchFinder::MatchResult &Result) { const auto *SizeOf = Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("sizeof"); - SourceLocation SizeOfLoc = SizeOf->getLocStart(); - auto Diag = diag(SizeOfLoc, "sizeof() doesn't return the size of the " - "container; did you mean .size()?"); - - // Don't generate fixes for macros. - if (SizeOfLoc.isMacroID()) - return; - - SourceLocation RParenLoc = SizeOf->getRParenLoc(); - - // sizeof argument is wrapped in a single ParenExpr. - const auto *Arg = cast<ParenExpr>(SizeOf->getArgumentExpr()); - - if (needsParens(Arg->getSubExpr())) { - Diag << FixItHint::CreateRemoval( - CharSourceRange::getTokenRange(SizeOfLoc, SizeOfLoc)) - << FixItHint::CreateInsertion(RParenLoc.getLocWithOffset(1), - ".size()"); - } else { - Diag << FixItHint::CreateRemoval( - CharSourceRange::getTokenRange(SizeOfLoc, Arg->getLParen())) - << FixItHint::CreateReplacement( - CharSourceRange::getTokenRange(RParenLoc, RParenLoc), - ".size()"); - } + auto Diag = + diag(SizeOf->getLocStart(), "sizeof() doesn't return the size of the " + "container; did you mean .size()?"); } } // namespace tidy diff --git a/clang-tools-extra/test/clang-tidy/misc-sizeof-container.cpp b/clang-tools-extra/test/clang-tidy/misc-sizeof-container.cpp index ca81bd2a43a..fc84b2b0af6 100644 --- a/clang-tools-extra/test/clang-tidy/misc-sizeof-container.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-sizeof-container.cpp @@ -65,19 +65,14 @@ void f() { int a = 42 + sizeof(s1); // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: sizeof() doesn't return the size of the container; did you mean .size()? [misc-sizeof-container] -// CHECK-FIXES: int a = 42 + s1.size(); a = 123 * sizeof(s2); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: sizeof() doesn't return the size -// CHECK-FIXES: a = 123 * s2.size(); a = 45 + sizeof(s2 + "asdf"); // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: sizeof() doesn't return the size -// CHECK-FIXES: a = 45 + (s2 + "asdf").size(); a = sizeof(v); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: sizeof() doesn't return the size -// CHECK-FIXES: a = v.size(); a = sizeof(std::vector<int>{}); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: sizeof() doesn't return the size -// CHECK-FIXES: a = std::vector<int>{}.size(); a = sizeof(a); a = sizeof(int); |

