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

Alexander Kornienko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 06:48:25 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:
> > 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).
> Interestingly it does warn about it, but only once, even if you have two different template specializations.
> 
> I tried to suppress this warning when the type being instantiated is a template argument type but no matter what I tried I could not get this to work. Is there a way to get this information from the MatchedDecl object or does one need to do something more complicated like going up the AST until a function definition is found and checking if it is a template specialization (presumably with TemplatedKind)? Any help would be appreciated.
If there are multiple warnings with the same message at the same location (clang-tidy/ClangTidyDiagnosticConsumer.cpp:745), they will be deduplicated. Thus, a random fix will probably be suggested. The proper way to filter out matches in template instantiations is to add `unless(isInTemplateInstantiation())` to the matcher.


Repository:
  rCTE Clang Tools Extra

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64671/new/

https://reviews.llvm.org/D64671





More information about the cfe-commits mailing list