Skip to content

Propose to exchange ranges only when it is safe to do so #14432

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 2 commits into
base: master
Choose a base branch
from

Conversation

samueltardieu
Copy link
Contributor

@samueltardieu samueltardieu commented Mar 18, 2025

To avoid false positives, the range_plus_one and range_minus_one lints will restrict themselves to situations where the iterator types can be easily switched from exclusive to inclusive or vice-versa. This includes situations where the range is used as an iterator, or is used for indexing.

On the other hand, assignments of the range to variables, including automatically typed ones or wildcards, will no longer trigger the lint. However, the cases where such an assignment would benefit from the lint are probably rare.

In a second commit, the range_plus_one and range_minus_one logic are unified, in order to properly emit parentheses around the suggestion when needed.

Fix #3307
Fix #9908

changelog: [range_plus_one, range_minus_one]: restrict lint to cases where it is safe to switch the range type

Edit: as a consequence, this led to the removal of three #[expect(clippy::range_plus_one)] in the Clippy sources to avoid those false positives.

@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2025

r? @llogiq

rustbot has assigned @llogiq.
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 Mar 18, 2025
@samueltardieu
Copy link
Contributor Author

This could also be extended to situations where the ranges are used as an argument to a function/method call which accepts an impl or dyn trait implemented by all kind of ranges:

  • Letting this as a future enhancement removes current false positives.
  • Doing it in this PR limits the number of places where the lint triggered before but won't trigger now.

@samueltardieu
Copy link
Contributor Author

Rebased on master

@samueltardieu
Copy link
Contributor Author

Putting to draft, I want to double-check the lintcheck, it looks like we get some false negatives there.
@rustbot author

@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 Mar 31, 2025
@samueltardieu samueltardieu marked this pull request as draft March 31, 2025 14:43
To avoid false positives, the `range_plus_one` and `range_minus_one` lints
will restrict themselves to situations where the iterator types can be easily
switched from exclusive to inclusive or vice-versa. This includes situations
where the range is used as an iterator, or is used for indexing. Also,
when the range is used as a function or method call argument and the
parameter type is generic over traits implemented by both kind of
ranges, the lint will trigger.

On the other hand, assignments of the range to variables, including
automatically typed ones or wildcards, will no longer trigger the lint.
However, the cases where such an assignment would benefit from the lint
are probably rare.
@samueltardieu samueltardieu marked this pull request as ready for review March 31, 2025 16:40
@samueltardieu
Copy link
Contributor Author

Much better. I added checking if the range to be modified is a method or function call argument, and if the target range would be suitable as well.

@rustbot review

@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 Mar 31, 2025
@samueltardieu
Copy link
Contributor Author

r? clippy

@samueltardieu
Copy link
Contributor Author

samueltardieu commented Apr 15, 2025

I added a second commit to fix an issue with parentheses lacking from some suggestions identified in a comment made tonight in #9908.

Those lints share the detection logic structure, but differed, probably
because of an oversight, in lint emission: only one of them would take
care of emitting parentheses around the suggestion if required.

Factor the code into one function which checks for the right condition
and emits the lint in an identical way.
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
4 participants