llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Duplicate warning diagnostic produced by compiler for same code

Open dyung opened this issue 2 months ago • 4 comments

Consider the following reduced test case originally derived from clang/test/SemaTemplate/instantiate-member-initializers.cpp:

template <class> void C2() { ""; }
template void C2< int >();

When compiled, the compiler produces the warning diagnostic twice https://godbolt.org/z/McYKrb7zj:


clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
<source>:1:30: warning: expression result unused [-Wunused-value]
    1 | template <class> void C2() { ""; }
      |                              ^~
<source>:1:30: warning: expression result unused [-Wunused-value]
    1 | template <class> void C2() { ""; }
      |                              ^~
<source>:2:15: note: in instantiation of function template specialization 'C2<int>' requested here
    2 | template void C2< int >();
      |               ^
2 warnings generated.
Compiler returned: 0

dyung avatar Sep 17 '26 23:09 dyung

Goes back to clang-9

shafik avatar Sep 18 '26 07:09 shafik

Hi! I'd like to work on this

danielle060821 avatar Sep 18 '26 20:09 danielle060821

Hi! I'd like to work on this

We don't assign issues until after there is a PR and we see it is moving in the right direction but saying you are working on it can help prevent duplication of effort.

shafik avatar Sep 18 '26 23:09 shafik

Dug into this a bit, turns out it's not a one-off bug, there's a related open issue (#55057) about the same "checked once per template pattern, once per instantiation" thing happening with -Wreturn-type too, seemingly unresolved since 2022.

I tried a few guards on Sema::DiagnoseUnusedExprResult (checking CurrentInstantiationScope, inTemplateInstantiation(), then a location-based dedup keyed on (DiagID, SourceLocation)). Each one either didn't suppress the dupe, or over-suppressed and broke a whole bunch of other tests (warn-unused-value.cpp, coroutines.cpp, typo-correction.cpp, etc.) because some of these diagnostics (the [[nodiscard]]/warn_unused_result ones especially) can legitimately differ per-instantiation, so you can't just dedup by location safely.

Will make a PR if I land on something that passes clean, posting in case someone's already looked at this class of bug and has pointers, since it doesn't seem like a quick fix.

StarOne01 avatar Sep 19 '26 05:09 StarOne01