diff options
-rw-r--r-- | clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp | 12 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp | 2 |
2 files changed, 10 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp b/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp index 218a2370632..98c6246b836 100644 --- a/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/MemsetZeroLengthCheck.cpp @@ -57,13 +57,17 @@ void MemsetZeroLengthCheck::check(const MatchFinder::MatchResult &Result) { const Expr *Arg2 = Call->getArg(2); // Try to evaluate the second argument so we can also find values that are not - // just literals. We don't emit a warning if the second argument also - // evaluates to zero. + // just literals. llvm::APSInt Value1, Value2; - if (!Arg2->EvaluateAsInt(Value2, *Result.Context) || Value2 != 0 || - (Arg1->EvaluateAsInt(Value1, *Result.Context) && Value1 == 0)) + if (!Arg2->EvaluateAsInt(Value2, *Result.Context) || Value2 != 0) return; + // If both arguments evaluate to zero emit a warning without fix suggestions. + if (Arg1->EvaluateAsInt(Value1, *Result.Context) && Value1 == 0) { + diag(Call->getLocStart(), "memset of size zero"); + return; + } + // Emit a warning and fix-its to swap the arguments. auto D = diag(Call->getLocStart(), "memset of size zero, potentially swapped arguments"); diff --git a/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp b/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp index 262eb51d625..5b2327148e1 100644 --- a/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp +++ b/clang-tools-extra/test/clang-tidy/google-memset-zero-length.cpp @@ -46,6 +46,8 @@ void foo(void *a, int xsize, int ysize) { memset(a, -1, sizeof(int)); memset(a, 0xcd, 1); memset(a, v, 0); +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: memset of size zero +// CHECK-FIXES: memset(a, v, 0); memtmpl<0>(); } |