Skip to content
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

fix [manual_flatten] help texts order #9156

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions clippy_lints/src/loops/manual_flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,32 @@ pub(super) fn check<'tcx>(
_ => ""
};

let sugg = format!("{arg_snippet}{copied}.flatten()");

// If suggestion is not a one-liner, it won't be shown inline within the error message. In that case,
// it will be shown in the extra `help` message at the end, which is why the first `help_msg` needs
// to refer to the correct relative position of the suggestion.
let help_msg = if sugg.contains('\n') {
"remove the `if let` statement in the for loop and then..."
} else {
"...and remove the `if let` statement in the for loop"
};

span_lint_and_then(
cx,
MANUAL_FLATTEN,
span,
&msg,
|diag| {
let sugg = format!("{}{}.flatten()", arg_snippet, copied);
diag.span_suggestion(
arg.span,
"try",
sugg,
Applicability::MaybeIncorrect,
applicability,
);
diag.span_help(
inner_expr.span,
"...and remove the `if let` statement in the for loop",
help_msg,
);
}
);
Expand Down
1 change: 1 addition & 0 deletions tests/lint_message_convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Message {
r".*remove .*the return type...",
r"note: Clippy version: .*",
r"the compiler unexpectedly panicked. this is a bug.",
r"remove the `if let` statement in the for loop and then...",
])
.unwrap();

Expand Down
16 changes: 16 additions & 0 deletions tests/ui/manual_flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ fn main() {
for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
println!("{}", n);
}

run_unformatted_tests();
}

#[rustfmt::skip]
fn run_unformatted_tests() {
// Skip rustfmt here on purpose so the suggestion does not fit in one line
for n in vec![
Some(1),
Some(2),
Some(3)
].iter() {
if let Some(n) = n {
println!("{:?}", n);
}
}
}
30 changes: 29 additions & 1 deletion tests/ui/manual_flatten.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,33 @@ LL | | println!("{:?}", n);
LL | | }
| |_________^

error: aborting due to 8 previous errors
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> $DIR/manual_flatten.rs:116:5
|
LL | / for n in vec![
LL | | Some(1),
LL | | Some(2),
LL | | Some(3)
... |
LL | | }
LL | | }
| |_____^
|
help: remove the `if let` statement in the for loop and then...
--> $DIR/manual_flatten.rs:121:9
|
LL | / if let Some(n) = n {
LL | | println!("{:?}", n);
LL | | }
| |_________^
help: try
|
LL ~ for n in vec![
LL + Some(1),
LL + Some(2),
LL + Some(3)
LL ~ ].iter().flatten() {
|

error: aborting due to 9 previous errors