-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
suggest alternatives to iterate an array of ranges #11862
Merged
bors
merged 5 commits into
rust-lang:master
from
christophbeberweil:7125-single-element-loop-over-range
Nov 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
447edf9
suggest alternatives to iterate an array of ranges
christophbeberweil c58d692
fix: 7125 update lint applicability to Unspecified
christophbeberweil 2512341
feat: 7125 shorten lint text
christophbeberweil bce869f
fix: 7125 lint message should start with a small letter
christophbeberweil f9c6335
feat: 7125 code snippets are wrapped in backticks
christophbeberweil File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
use super::SINGLE_ELEMENT_LOOP; | ||||||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||||||
use clippy_utils::source::{indent_of, snippet_with_applicability}; | ||||||
use clippy_utils::source::{indent_of, snippet, snippet_with_applicability}; | ||||||
use clippy_utils::visitors::contains_break_or_continue; | ||||||
use rustc_ast::util::parser::PREC_PREFIX; | ||||||
use rustc_ast::Mutability; | ||||||
|
@@ -87,14 +87,27 @@ pub(super) fn check<'tcx>( | |||||
arg_snip = format!("({arg_snip})").into(); | ||||||
} | ||||||
|
||||||
span_lint_and_sugg( | ||||||
cx, | ||||||
SINGLE_ELEMENT_LOOP, | ||||||
expr.span, | ||||||
"for loop over a single element", | ||||||
"try", | ||||||
format!("{{\n{indent}let {pat_snip} = {prefix}{arg_snip};{block_str}}}"), | ||||||
applicability, | ||||||
); | ||||||
if clippy_utils::higher::Range::hir(arg_expression).is_some() { | ||||||
let sugg = snippet(cx, arg_expression.span, ".."); | ||||||
span_lint_and_sugg( | ||||||
cx, | ||||||
SINGLE_ELEMENT_LOOP, | ||||||
arg.span, | ||||||
"for loop over a single range inside an array, rather than iterating over the elements in the range directly", | ||||||
"did you mean to iterate over the range instead?", | ||||||
sugg.to_string(), | ||||||
applicability, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
); | ||||||
} else { | ||||||
span_lint_and_sugg( | ||||||
cx, | ||||||
SINGLE_ELEMENT_LOOP, | ||||||
expr.span, | ||||||
"for loop over a single element", | ||||||
"try", | ||||||
format!("{{\n{indent}let {pat_snip} = {prefix}{arg_snip};{block_str}}}"), | ||||||
applicability, | ||||||
); | ||||||
} | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can find a shorter way to phrase this? I like the clarity though. Maybe something like "This loops only once with <loop pattern> being <range expr>", which is admittedly harder to construct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed a lot about the message of this lint, but opted for a longer and more verbose one. I adjusted it to contain your version, which is shorter, but now the text "0..5" is written twice in the lint message, which feels redundant to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it as "item" (the looping variable name) and "0..5", rather than 0..5 twice. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the current output of the lint:
"0-5" is mentioned in the lint error message and in the suggestion. I thing mentioning it once should be enough, but mentioning it twice will probably not hurt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and with a lowercase "t" the error message actually passes the tests, which were failing with my commit from earlier today. :)