This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Ignore concepts in `misc-redundant-expression`
ClosedPublic

Authored by Izaron on Mar 19 2022, 1:16 PM.

Details

Summary

The checker should ignore requirement expressions inside concept
definitions, because redundant expressions still make sense here

Fixes https://github.com/llvm/llvm-project/issues/54114

Diff Detail

Event Timeline

Izaron created this revision.Mar 19 2022, 1:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 19 2022, 1:17 PM
Izaron requested review of this revision.Mar 19 2022, 1:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 19 2022, 1:17 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added subscribers: sammccall, alexfh.

Thanks for the fix, can you also add a release note for it?

I'm a bit worried about using hasAncestor() for this; that has a tendency to do surprising things in addition to being expensive because it's very greedy. However, I can't see a situation in which it's going to be wrong, because the body of the requires expression is always unevaluated (so we really don't need to worry about nonsense like defining a lambda in the requires body, and then defining a class in the lambda, and having a redundant expression we care to diagnose in a member function of that class).

@alexfh or @sammccall -- do you see any concerns with this use of hasAncestor()?

I don't actually know a great deal about matcher performance :-( I wish I did.
I think this is pretty much in line with how plenty of check matchers already work though, in particular unless(isInTemplateInstantiation()) is basically the same thing and is used in many places including this check.
So I wouldn't be particularly worried.

(Obviously from first principles you'd prefer to prune the subtree rather than match within it and then walk up the parent chain, but I don't think matchers supports this pattern well).

aaron.ballman accepted this revision.Mar 25 2022, 7:17 AM

I don't actually know a great deal about matcher performance :-( I wish I did.
I think this is pretty much in line with how plenty of check matchers already work though, in particular unless(isInTemplateInstantiation()) is basically the same thing and is used in many places including this check.
So I wouldn't be particularly worried.

(Obviously from first principles you'd prefer to prune the subtree rather than match within it and then walk up the parent chain, but I don't think matchers supports this pattern well).

Okie dokie, thanks for weighing in! This LGTM (feel free to add the release note when landing). If @alexfh has some technical concerns with hasAncestor(), we can always revert and address them once we know more.

This revision is now accepted and ready to land.Mar 25 2022, 7:17 AM

Okie dokie, thanks for weighing in! This LGTM (feel free to add the release note when landing). If @alexfh has some technical concerns with hasAncestor(), we can always revert and address them once we know more.

I feel like this is another issue which could be solved with traversal scopes. isInEvaluationContext This traversal scope wouldn't traverse things like concepts, static_asserts(?), Noexcept & explicit specifiers, sizeof, decltype etc.

Izaron updated this revision to Diff 460209.Sep 14 2022, 1:46 PM

Add release note

njames93 requested changes to this revision.Sep 14 2022, 10:02 PM

Just address that test issue then it'll be good

clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
1

This is removing test coverage for previous language versions unnecessarily. Can you create a new test file redundant-expression-cxx20 instead and move this new test case into there.

This revision now requires changes to proceed.Sep 14 2022, 10:02 PM
Izaron updated this revision to Diff 460976.Sep 17 2022, 12:20 AM

Created redundant-expression-cxx20.cpp

Izaron updated this revision to Diff 460990.Sep 17 2022, 4:42 AM

The new file was failing with message

CHECK-FIXES, CHECK-MESSAGES or CHECK-NOTES not found in the input

So I had to add an additional urrelevant check.
I added a check for consteval function (there were no such tests previously).

The new file was failing with message

CHECK-FIXES, CHECK-MESSAGES or CHECK-NOTES not found in the input

So I had to add an additional urrelevant check.
I added a check for consteval function (there were no such tests previously).

You don't need the unrelated tests, you can use a special run line that will check that no warnings are emitted.
// RUN: clang-tidy %s --checks=-*,misc-redundant-expression -- | count 0

Izaron updated this revision to Diff 461003.Sep 17 2022, 6:47 AM

Fix test with count 0, thanks to @njames93 !

Izaron added a comment.Oct 7 2022, 2:53 PM

Hi! A friendly ping =)

njames93 accepted this revision.Oct 8 2022, 1:48 AM

LGTM, just one small nit.

clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-cxx20.cpp
3 ↗(On Diff #461003)

This comment isn't strictly necessary

This revision is now accepted and ready to land.Oct 8 2022, 1:48 AM
Izaron updated this revision to Diff 466271.Oct 8 2022, 2:47 AM

Removed redundant comment

Izaron marked an inline comment as done.Oct 8 2022, 2:47 AM
This revision was automatically updated to reflect the committed changes.