This is an archive of the discontinued LLVM Phabricator instance.

[AST][RecoveryExpr] Fix a bogus unused diagnostic when the type is preserved.
ClosedPublic

Authored by hokein on Aug 11 2020, 2:46 AM.

Diff Detail

Event Timeline

hokein created this revision.Aug 11 2020, 2:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2020, 2:46 AM
hokein requested review of this revision.Aug 11 2020, 2:46 AM
sammccall added inline comments.Aug 11 2020, 6:49 AM
clang/lib/Sema/SemaStmt.cpp
353 ↗(On Diff #284621)

This seems like a very specific patch to a special case of a potentially more general problem...

clang/test/SemaCXX/recovery-expr-type.cpp
88

(nit: I think this is -Wunused but not -Wunused-variable)

88

what makes you think this is firing on the *inner* expression? The diagnostic location is that of the outer expression. I think the existing logic is pretty close to correct (and the diagnostic is actually correct here, albeit through luck), and if we want to change it, the right fix belongs somewhere else.

The warning doesn't fire for just Derived(); - isUnusedResultAWarning returns false for RecoveryExpr. Nor does it fire for (Derived()); - isUnusedResultAWarning looks inside the paren expr and gets the same result for the contents.

For the case in this test, we hit the CXXFunctionalCastExpr/CStyleExprCast case of isUnusedResultAWarning. It says:

If this is a cast to a constructor conversion, check the operand.
Otherwise, the result of the cast is unused.

But this is a bit misleading: given Foo(2+2) the "operand" isn't 2+2, but rather a CXXConstructExpr. And isUnusedResultAWarning returns true for this case (usually).

Back to the case at hand: we diverge because the cast type is Dependent instead of ConstructorConversion.
This seems like it could be a bug - in this configuration the operand is not type-dependent so why can't it resolve the overload?
But in any case, I don't think isUnusedResultAWarning should be assuming a dependent cast is safe to warn on - rather either return false or delegate to the inner expr (which in this case is a RecoveryExpr).

hokein updated this revision to Diff 284742.Aug 11 2020, 8:19 AM

address review comment:
a dependent-kind cast expr should not be warned about the unused result.

clang/test/SemaCXX/recovery-expr-type.cpp
88

oh, my bad. I misunderstood this part of code -- I missed the critical function isUnusedResultAWarning, I saw it when reading this part of code, but I skipped it because the function name made me think "this is a trivial function that just detects whether the -Wunused flag is on".

I think you analysis is right here. Thanks!

what makes you think this is firing on the *inner* expression? The diagnostic location is that of the outer expression.

hmm, I was confused by the ~annotation of the diagnostic.

(Derived(Derived()));
  ^      ~~~~~~~
sammccall accepted this revision.Aug 21 2020, 4:08 AM
sammccall added inline comments.
clang/test/SemaCXX/recovery-expr-type.cpp
80

nit: can the testcase be simplified?
Isn't struct T { T() = delete; }; enough (with no inheritance)

This revision is now accepted and ready to land.Aug 21 2020, 4:08 AM
hokein updated this revision to Diff 287025.Aug 21 2020, 6:48 AM
hokein marked an inline comment as done.

simplify the testcase.

This revision was landed with ongoing or failed builds.Aug 21 2020, 6:49 AM
This revision was automatically updated to reflect the committed changes.