Skip to content

fix: zero_repeat_side_effects generic type needed#16681

Open
paulmialane wants to merge 1 commit intorust-lang:masterfrom
paulmialane:fix/zero_repeat_side_effects-generic-type-needed
Open

fix: zero_repeat_side_effects generic type needed#16681
paulmialane wants to merge 1 commit intorust-lang:masterfrom
paulmialane:fix/zero_repeat_side_effects-generic-type-needed

Conversation

@paulmialane
Copy link
Contributor

@paulmialane paulmialane commented Mar 7, 2026

Fix zero_repeat_side_effects emitting broken suggestions for generic types

Fixes #16474

What's wrong

The zero_repeat_side_effects lint suggests splitting

let mut entries = vec![Entry::default(); 0];

into:

Entry::default();
let mut entries: Vec<Entry<usize>> = vec![];

This works fine for simple types, but breaks when the element type has generic parameters, causing:

error[E0283]: type annotations needed
  --> src/main.rs:10:5
   |
   | Entry::default();
   | ^^^^^ cannot infer type for type parameter `T`

The fix

I was looking to fix this by suggesting let _: T = expr; instead of a bare expr;.

This applies to the let_stmt_suggestion and assign_expr_suggestion code paths (both the curly and non-curly branches).

I think the Node::Stmt case (bare expression statement like vec![f(); 0];) does not need this fix because if T were ambiguous there, the original code would already fail to compile.

Remaining problems

While writting the tests, I realized my implementation introduces a new broken suggestion.

When the element type is defined inside a function (for instance issue_16474(), Ty::to_string emits a path like issue_16474::Entry<usize> which is invalid in the suggestion since issue_16474 is a function, not a module.

I see two different approaches:

  1. Detect that the type is defined in a function and only emit a help message.
  2. Or find a way to trim the function name in the suggestion.

In both case, I don't really know the best way to do this, and I wanted a reviewer's opinion on the right approach before going further.

Thank you !

changelog: [zero_repeat_side_effects]: fix suggestion causes error for generic types

@paulmialane paulmialane marked this pull request as ready for review March 7, 2026 14:58
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2026

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zero_repeat_side_effects generic type needed

3 participants