rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

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

Open NullSpaceBoy opened this issue 2 years ago • 5 comments

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

NullSpaceBoy avatar Jun 21 '24 05:06 NullSpaceBoy

@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.

xFrednet avatar Jul 03 '24 18:07 xFrednet

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

NullSpaceBoy avatar Jul 22 '24 14:07 NullSpaceBoy

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 avatar Jul 22 '24 23:07 y21

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

NullSpaceBoy avatar Jul 30 '24 13:07 NullSpaceBoy

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

y21 avatar Aug 10 '24 16:08 y21

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.

y21 avatar Aug 30 '24 20:08 y21

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

NullSpaceBoy avatar Sep 01 '24 05:09 NullSpaceBoy

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.

y21 avatar Sep 01 '24 09:09 y21

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.

NullSpaceBoy avatar Sep 01 '24 14:09 NullSpaceBoy

@rustbot ready

NullSpaceBoy avatar Feb 15 '25 09:02 NullSpaceBoy

@y21 Thanks for the ping, I fully trust your review! So, don't wait for me to take a second look at it :)

xFrednet avatar Mar 16 '25 14:03 xFrednet

:umbrella: The latest upstream changes (possibly d28d2344d000aa96bef729cf408731f952f71fb0) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Mar 31 '25 23:03 rustbot