Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty_enum_variants_with_brackets: Do not lint reachable enums and enum variants used as functions in the same crate #12971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ARandomDev99
Copy link
Contributor

Fixes #12551

changelog: [empty_enum_variants_with_brackets]: Do not lint reachable enums or enums which are used as functions within the same crate.

r? @xFrednet

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 21, 2024
@ARandomDev99 ARandomDev99 force-pushed the issue-12551 branch 3 times, most recently from faeed5b to 895e669 Compare June 22, 2024 04:49
@ARandomDev99 ARandomDev99 requested a review from y21 June 22, 2024 04:50
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
tests/ui/empty_enum_variants_with_brackets.rs Show resolved Hide resolved
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
@xFrednet
Copy link
Member

xFrednet commented Jul 3, 2024

@y21 Do you want to take over the review, since you already seems to be in the domain? Otherwise, I'm happy to take it.

Assuming that @ARandomDev99 didn't have a specific reason to r? me in the description.

@rust-lang rust-lang deleted a comment from rustbot Jul 3, 2024
@y21 y21 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 7, 2024
@ARandomDev99
Copy link
Contributor Author

I'm having trouble emitting a multipart suggestion that removes the parentheses only.

@y21
Copy link
Member

y21 commented Jul 22, 2024

You can make that is_callee function return an Option<Span>. Probably makes sense to also rename the function at that point (maybe call_parentheses_span?).

parent.span.with_lo(expr.span.hi()) should give you the parentheses only so you can return that from the function and push it into the vec

@y21 y21 self-assigned this Jul 22, 2024
@ARandomDev99 ARandomDev99 force-pushed the issue-12551 branch 2 times, most recently from 5aa0947 to cf507a9 Compare July 23, 2024 11:33
@ARandomDev99
Copy link
Contributor Author

ARandomDev99 commented Jul 30, 2024

I just ran cargo test after cloning my fork into a new directory. All the tests passed. I wonder why it failed on GitHub.

@y21
Copy link
Member

y21 commented Aug 10, 2024

Sorry, completely missed the notification here. Does it repro if you rebase onto master? There have been a bunch of changes to uitest since then

@ARandomDev99 ARandomDev99 force-pushed the issue-12551 branch 2 times, most recently from 13d2d8c to 2916ed9 Compare August 14, 2024 10:47
@ARandomDev99 ARandomDev99 requested a review from y21 August 14, 2024 10:58
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
clippy_lints/src/empty_with_brackets.rs Outdated Show resolved Hide resolved
@ARandomDev99 ARandomDev99 force-pushed the issue-12551 branch 2 times, most recently from e46009d to 7cc588c Compare August 15, 2024 14:15
@ARandomDev99 ARandomDev99 requested a review from y21 August 15, 2024 14:44
@y21
Copy link
Member

y21 commented Aug 30, 2024

I just looked at the lintcheck CI results and looks like there's a fair amount of false positives: https://github.com/rust-lang/rust-clippy/actions/runs/10405234866
E.g.

warning: enum variant has empty brackets
  --> target/lintcheck/sources/addr2line-0.24.0/src/frame.rs:40:11
   |
40 |     Frames(FrameIterFrames<'ctx, R>),
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: remove the brackets
   |
40 ~     Frames,
41 | }

I think this might be because we're inserting new Usage::Unused into the map in check_expr, regardless of whether the variant actually has no fields. I imagine we could simply add a new variant to Usage for "found a use but not its definition", which is ignored in check_crate_post and "upgraded" to Usage::Unused in check_variant when we've verified it has no fields?
Or we could also add the has_no_fields condition in check_expr as well (before inserting Usage::Unused), essentially mirroring check_variant. Might be easier.

@ARandomDev99 ARandomDev99 force-pushed the issue-12551 branch 3 times, most recently from 36321e5 to 9987594 Compare August 31, 2024 16:52
@ARandomDev99
Copy link
Contributor Author

I fail to understand why false positives like this show up. There's even passing tests for exactly this situation.

}) => {
redundant_use_sites.push(parentheses_span);
},
None if has_no_fields(cx, None, parentheses_span) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pass None here instead of the variant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the span that's passed here is the wrong one. It expects the parentheses at the variant definition site, but this is passing the parentheses of the function call.

It doesn't explain the false positives, but passing this span makes has_no_fields check for the wrong thing (since it uses the span for checking for #[cfg]s).
TBH, I'd just get rid of the empty_parentheses function entirely and use !span_contains_cfg() instead.

@y21
Copy link
Member

y21 commented Sep 1, 2024

The false positives seem to happen when the constructor call comes from a derive macro like #[derive(Clone)] with its output tokens all having the same span, which makes the parentheses_span empty.
So a test case that runs into it would be something like

#[derive(Clone)]
enum Foo {
  Variant(i32)
}

Thinking more about it, it might actually be better and easier to have a new Usage::NoDefinition { redundant_use_sites: Vec<Span> } or something as mentioned in my other comment (which is ignored in check_crate_post but would get turned into Usage::Unused in check_variant after having done all checks), so we wouldn't need to repeat the logic in check_expr. Otherwise one would need to remember to update both functions check_variant and check_expr whenever more logic is added.

@ARandomDev99 ARandomDev99 force-pushed the issue-12551 branch 2 times, most recently from a33082c to f08507f Compare September 1, 2024 14:38
@ARandomDev99
Copy link
Contributor Author

In addition to the changes you suggested, ensuring that expressions from macro expansions aren't included in redundant_use_sites seems to have fixed the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

empty_enum_variants_with_brackets is arguably not redundant
4 participants