[PATCH] D64671: [clang-tidy] New check: misc-init-local-variables

Alexander Kornienko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 16 05:18:07 PDT 2019

alexfh added inline comments.

Comment at: clang-tools-extra/clang-tidy/misc/InitLocalVariablesCheck.cpp:21
+  Finder->addMatcher(
+      varDecl(unless(hasInitializer(anything()))).bind("vardecl"), this);
jpakkane wrote:
> alexfh wrote:
> > I believe, this should skip matches within template instantiations. Consider this code:
> > ```
> > template<typename T>
> > void f(T) { T t; }
> > void g() {
> >     f(0);
> >     f(0.0);
> > }
> > ```
> > 
> > What will the fix  be?
> I tested with the following function:
> ```
> template<typename T>
> void template_test_function() {
>   T t;
>   int uninitialized;
> }
> ```
> Currently it warns on the "uninitialized" variable regardless of whether the template is instantiated or not. If you call it with an int type, it will warn about variable t being uninitialized. If you call it with a, say, struct type, there is no warnings. Is this a reasonable approach?
And what happens, if there are multiple instantiations of the same template, each of them requiring a different fix? Can you try the check with my example above (and maybe also add `f("");`inside `g()`). I believe, the check will produce multiple warnings with conflicting fixes (and each of them will be wrong, btw).

Comment at: clang-tools-extra/clang-tidy/misc/InitLocalVariablesCheck.cpp:32
+  StringRef VarName = MatchedDecl->getName();
+  if (VarName.empty() || VarName.front() == '_') {
+    // Some standard library methods such as "be64toh" are implemented
jpakkane wrote:
> alexfh wrote:
> > Should this just disallow all fixes within macros? Maybe warnings as well.
> I can change that, seems reasonable. Should it still retain this check, though? One would imagine there are other ways of getting variables whose names begin with an underscore.
I don't know, whether the check for leading underscore will still be valuable. I don't think there's a valid reason why variables with a leading underscore in their names should in general not be initialized.

  rCTE Clang Tools Extra



More information about the cfe-commits mailing list