[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 21 09:39:45 PST 2023


================
@@ -54,6 +55,7 @@ class MiscModule : public ClangTidyModule {
     CheckFactories.registerCheck<MisleadingIdentifierCheck>(
         "misc-misleading-identifier");
     CheckFactories.registerCheck<MisplacedConstCheck>("misc-misplaced-const");
+    CheckFactories.registerCheck<MustUseCheck>("misc-must-use");
----------------
PiotrZSL wrote:

I do not like check name, it's to short, and doesn't say what is not used (member, var, global, function, class ?).
Second this should eb a bugprone check with preconfigured things like std::mutex, std::future, and probably few other.
Third i personally got check called bugprone-unused-local-non-trivial-variable, that is implemented like this:
```
void BugproneUnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) {
  Finder->addMatcher(varDecl(unless(isExpansionInSystemHeader()),
                             unless(isImplicit()),
                             unless(parmVarDecl()),
                             unless(decompositionDecl()),
                             unless(isExceptionVariable()),
                             hasLocalStorage(),
                             isDefinition(),
                             unless(hasType(namedDecl(matchesName(ExcludeTypeRegex)))),
                             unless(hasType(qualType(references(namedDecl(matchesName(ExcludeTypeRegex)))))),
                             decl().bind("var"),
                                 hasAncestor(functionDecl(unless(hasDescendant(declRefExpr(to(decl(equalsBoundNode("var"))))))))
                            ).bind("var"), this);
}

void BugproneUnusedLocalNonTrivialVariableCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("var");

  auto type = MatchedDecl->getType();
  if (type.isTrivialType(*Result.Context) or type.isTriviallyCopyableType(*Result.Context)) return;

  diag(MatchedDecl->getLocation(), "unused local variable %0 of non trivial type %1, consider removing")
      << MatchedDecl << type;
}

It's not perfect, but what I'm thinking, is if this check and that shoudn't be a single check with inclusion/exclusion settings, so it could be configured to warn on all or just on some limited scope.
```



https://github.com/llvm/llvm-project/pull/76101


More information about the cfe-commits mailing list