Skip to content

Allow more lazy evaluations#15633

Open
dylni wants to merge 1 commit intorust-lang:masterfrom
dylni:allow-more-lazy-evaluations
Open

Allow more lazy evaluations#15633
dylni wants to merge 1 commit intorust-lang:masterfrom
dylni:allow-more-lazy-evaluations

Conversation

@dylni
Copy link
Copy Markdown

@dylni dylni commented Sep 8, 2025

Update unnecessary_lazy_evaluations to allow closures for trivial functions according to the developer's preference.

The current list of trivial functions is is_empty, len, and as_*. This list cannot be updated without causing churn, because e.g., calls to unwrap_or_else and unwrap_or would need to be swapped for each addition and removal. This list is also undocumented and not based on actual method complexity, but repositories need to follow it to be compliant with this default lint. Leaving these trivial functions up to developer preference allows developers to not need to think about which functions Clippy considers lazy.

A configuration could be added later to re-enable this without any backward-compatibility concern, but it should not be enforced by default.

changelog: [unnecessary_lazy_evaluations]: allow optional closure for trivial functions

Fixes: #8109

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Sep 8, 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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 8, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Sep 8, 2025

Lintcheck changes for de1028a

Lint Added Removed Changed
clippy::if_then_some_else_none 0 0 3
clippy::option_if_let_else 0 0 2
clippy::unnecessary_lazy_evaluations 0 12 0

This comment will be updated if you push new changes

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Sep 18, 2025

☔ The latest upstream changes (possibly e1130b6) made this pull request unmergeable. Please resolve the merge conflicts.

@Jarcho
Copy link
Copy Markdown
Contributor

Jarcho commented Sep 25, 2025

This is not a good way to fix unnecessary_lazy_evaluations. The eager_or_lazy check is used by a lot a lints that will have a regression.

This is not saying there's nothing to fix here. unnecessary_lazy_evaluations should be far less aggressive in general, but it needs to be something targeted at that lint in particular.

@dylni
Copy link
Copy Markdown
Author

dylni commented Nov 29, 2025

@Jarcho Could you provide an example for me to consider? At least as far as I see this change, it only updates an Eager decision to NoChange, which should only accept more styles than previous. The related lints would also rightfully be updated to consider that a developer may intend for them to be lazily evaluated

The only change that I would expect to arguably be breaking would be recommendations, which as shown in the tests, now may suggest to use a closure for a simple function call. This would actually be more conservative as I see it

@Jarcho
Copy link
Copy Markdown
Contributor

Jarcho commented Jan 12, 2026

Something like:

if foo { Some(x.len()) }
else { None }

This should be suggesting foo.then_some(x.len()).

In such a case x.len() isn't done lazily because you wanted it to be lazy, but because it's the most obvious way to write the if. When switching to then or then_some the simpler choice is to use then_some without the closure. Most uses of eager_of_lazy are for this kind of decision.

unnecessary_lazy_evaluations is unique in that a closure has already been chosen as the form to use. Special handling for this and it's inverse are well justified since an explicit decision on which to use was already made.

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_lazy_evaluations warns about str::len

3 participants