Skip to content

Conversation

@ada4a
Copy link
Contributor

@ada4a ada4a commented Nov 21, 2025

Resurrects #13475

changelog: [unnecessary_fold]: lint on folds with Add::add/Mul::mul

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2025

r? @Jarcho

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

Comment on lines 45 to 55
// - the final expression in the body of a function with a simple return type
if let hir::Node::Block(block) = parent
&& let mut parents = cx.tcx.hir_parent_iter(block.hir_id).map(|(_, def_id)| def_id)
&& let Some(hir::Node::Expr(_)) = parents.next()
&& let Some(hir::Node::Item(enclosing_item)) = parents.next()
&& let hir::ItemKind::Fn { sig, .. } = enclosing_item.kind
&& let hir::FnRetTy::Return(fn_return_ty) = sig.decl.output
&& matches!(fn_return_ty.kind, hir::TyKind::Path(..))
{
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This and the previous checks are a subset of what using expr_use_ctxt would get you. You want something like:

if let Some(use_cx) = expr_use_ctxt(cx, expr)
  && use_cx.is_same_ctxt
  && let Some(ty) = use_cx.use_node(cx).defined_ty(cx)
{
  // check ty
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks for the tip! Changed

@rustbot rustbot 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 Nov 26, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 26, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@ada4a
Copy link
Contributor Author

ada4a commented Nov 27, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 27, 2025
gauravagerwala added a commit to gauravagerwala/rust-clippy that referenced this pull request Dec 7, 2025

// - the final expression in the body of a function with a simple return type
(ExprUseNode::Return(_), DefinedTy::Mir { ty: fn_return_ty, .. })
if fn_return_ty.skip_binder().is_simple_text() =>
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this supposed to be checking for? The return type of a function must be a fully defined type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is this supposed to be checking for?

Opaque types (like impl Add/Mul, see {add,mul}_turbofish_necessary), and generic types with trait bounds (something like fn sum<T: Add>(ts: &[T]) -> T), it seems

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change it to checking for opaque types then. Ty::walk will get you all the types you need to check.

The type of generic parameters can't be changed by inference so you can ignore those.

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.

3 participants