Skip to content

unnecessary_operation: don't suggest autofixes on composite types with fields of uncertain types #15460

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ada4a
Copy link
Contributor

@ada4a ada4a commented Aug 11, 2025

fixes #15381

changelog: [unnecessary_operation]: reduce applicability on exprs with uncertain types

@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2025

r? @y21

rustbot has assigned @y21.
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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 11, 2025
@ada4a ada4a force-pushed the unnecessary-operation-uncertain-exprs branch from 5477205 to dff9f73 Compare August 11, 2025 22:07
if let StmtKind::Semi(expr) = stmt.kind
&& !stmt.span.in_external_macro(cx.sess().source_map())
&& let ctxt = stmt.span.ctxt()
&& expr.span.ctxt() == ctxt
&& let Some(reduced) = reduce_expression(cx, expr)
&& let Some(reduced) = reduce_expression(cx, expr, &mut applicability)
&& reduced.iter().all(|e| e.span.ctxt() == ctxt)
Copy link
Contributor Author

@ada4a ada4a Aug 11, 2025

Choose a reason for hiding this comment

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

Kind of off-topic: I was forced to use the more verbose Vec::new() syntax in all the tests because, when I used vec![], this check would prevent the lint from firing. That doesn't sound quite right to me?

@ada4a ada4a changed the title unnecessary_operation: don't lint on composite types with fields of uncertain types unnecessary_operation: don't suggest autofixes on composite types with fields of uncertain types Aug 12, 2025
Comment on lines +332 to +334
applicability: &mut Applicability,
) -> Option<Vec<&'a Expr<'a>>> {
Copy link
Member

Choose a reason for hiding this comment

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

It might be better to return the Applicability/include it in the Option instead of mutating it. There is now an implicit invariant here that if None is returned, the applicability must not have been mutated (otherwise e.g. lines where we do .or_else(|| Some(vec![...])) may be using an irrelevant/outdated applicability from a nested expression that we ended up not using)

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That does sound better, yes. One problem is that it would make it more difficult to reduce recursively: we'll need to fold the returned applicabilities to choose the weakest one. I remember seeing an enum similar to Applicability somewhere which encoded that logic using the overloaded BitOr/BitOrAssign impls -- it would be cool to have that here as well

&& let Some(end) = range.end =>
{
if [start, end].into_iter().any(|e| expr_type_is_certain(cx, e)) {
Some([start, end].into_iter().collect())
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Some([start, end].into_iter().collect())
Some(vec![start, end])

Copy link
Member

Choose a reason for hiding this comment

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

But also, two more high level questions here:

  1. Should this also recurse into reduce_expression() with the start and end expressions of the range, to reduce it even further? I see this also isn't consistently done for some other kinds of expressions, like for array literals and struct expressions, so I'd also be fine with leaving it, but I'm not sure why it's done this way.

  2. Why do we return None/not reduce ranges at all if their type isn't certain, instead of only reducing the applicability like we do for structs in general? It still seems worth linting range expression statements that exist solely to make the inner expressions type-check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I think reducing too much in one go can end up creating some pretty confusing suggestions.. Although maybe they could make sense for the target audience of this lint (who I'm not really sure who is, except for people that have made a typo in a quick reproducer -- in which case the complete reduction might be actively counter-productive for understanding what the lint wants from them)
  2. I could totally make that change, yes. I guess it was just me too closely copying the structure of the regular Struct arm -- there, the suggestion is deleted when the type has an explicit Drop impl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've implemented 2. for now

@ada4a ada4a force-pushed the unnecessary-operation-uncertain-exprs branch from dff9f73 to 5198c64 Compare August 22, 2025 13:31
@ada4a ada4a force-pushed the unnecessary-operation-uncertain-exprs branch from 5198c64 to e783aea Compare August 22, 2025 13:51
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.

unnecessary-operation type can no longer be inferred
3 participants