[clang-tools-extra] [clang-tidy] fix false positive that floating point variable only used in increment expr in cert-flp30-c (PR #108706)
Julian Schmidt via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 15 15:38:56 PDT 2024
================
@@ -9,22 +9,33 @@
#include "FloatLoopCounter.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang::tidy::cert {
void FloatLoopCounter::registerMatchers(MatchFinder *Finder) {
- Finder->addMatcher(
- forStmt(hasIncrement(expr(hasType(realFloatingPointType())))).bind("for"),
- this);
+ Finder->addMatcher(forStmt(hasIncrement(forEachDescendant(
+ declRefExpr(hasType(realFloatingPointType()),
+ to(varDecl().bind("var"))))),
+ hasCondition(forEachDescendant(declRefExpr(
+ hasType(realFloatingPointType()),
+ to(varDecl(equalsBoundNode("var")))))))
+ .bind("for"),
+ this);
}
void FloatLoopCounter::check(const MatchFinder::MatchResult &Result) {
const auto *FS = Result.Nodes.getNodeAs<ForStmt>("for");
- diag(FS->getInc()->getExprLoc(), "loop induction expression should not have "
- "floating-point type");
+ diag(FS->getInc()->getBeginLoc(), "loop induction expression should not have "
+ "floating-point type");
----------------
5chmidti wrote:
Thoughts on binding both `declRefExpr`s and streaming their source ranges into the diagnostic?
```
for (float val = 0.1f; val <= 1.0f; val += 0.1f) {}
~~~ ^~~
```
(probably also changing the diag location to the `declRefExpr` in the increment as well)
You could also add the name of the bound variable to the diagnostic
https://github.com/llvm/llvm-project/pull/108706
More information about the cfe-commits
mailing list