Skip to content

Commit bc2feea

Browse files
committed
Auto merge of rust-lang#13117 - GuillaumeGomez:fix-single_element_loop-suggestion, r=Alexendoo
Fix wrong suggestion for `single_element_loop` where parens were missing Fixes rust-lang/rust-clippy#12782. changelog: Fix missing parens in `single_element_loop` suggestion
2 parents 073f9d9 + 8dd310d commit bc2feea

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

clippy_lints/src/loops/single_element_loop.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::visitors::contains_break_or_continue;
55
use rustc_ast::util::parser::PREC_PREFIX;
66
use rustc_ast::Mutability;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{is_range_literal, BorrowKind, Expr, ExprKind, Pat};
8+
use rustc_hir::{is_range_literal, BorrowKind, Expr, ExprKind, Pat, PatKind};
99
use rustc_lint::LateContext;
1010
use rustc_span::edition::Edition;
1111
use rustc_span::sym;
@@ -70,7 +70,10 @@ pub(super) fn check<'tcx>(
7070
&& !contains_break_or_continue(body)
7171
{
7272
let mut applicability = Applicability::MachineApplicable;
73-
let pat_snip = snippet_with_applicability(cx, pat.span, "..", &mut applicability);
73+
let mut pat_snip = snippet_with_applicability(cx, pat.span, "..", &mut applicability);
74+
if matches!(pat.kind, PatKind::Or(..)) {
75+
pat_snip = format!("({pat_snip})").into();
76+
}
7477
let mut arg_snip = snippet_with_applicability(cx, arg_expression.span, "..", &mut applicability);
7578
let mut block_str = snippet_with_applicability(cx, block.span, "..", &mut applicability).into_owned();
7679
block_str.remove(0);

tests/ui/single_element_loop.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ fn main() {
5757
}
5858
};
5959
}
60+
61+
// Should lint with correct suggestion (issue #12782)
62+
let res_void: Result<bool, bool> = Ok(true);
63+
64+
{
65+
let (Ok(mut _x) | Err(mut _x)) = res_void;
66+
let ptr: *const bool = std::ptr::null();
67+
}
6068
}

tests/ui/single_element_loop.rs

+7
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ fn main() {
5454
}
5555
};
5656
}
57+
58+
// Should lint with correct suggestion (issue #12782)
59+
let res_void: Result<bool, bool> = Ok(true);
60+
61+
for (Ok(mut _x) | Err(mut _x)) in [res_void] {
62+
let ptr: *const bool = std::ptr::null();
63+
}
5764
}

tests/ui/single_element_loop.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,21 @@ LL + };
8383
LL + }
8484
|
8585

86-
error: aborting due to 7 previous errors
86+
error: for loop over a single element
87+
--> tests/ui/single_element_loop.rs:61:5
88+
|
89+
LL | / for (Ok(mut _x) | Err(mut _x)) in [res_void] {
90+
LL | | let ptr: *const bool = std::ptr::null();
91+
LL | | }
92+
| |_____^
93+
|
94+
help: try
95+
|
96+
LL ~ {
97+
LL + let (Ok(mut _x) | Err(mut _x)) = res_void;
98+
LL + let ptr: *const bool = std::ptr::null();
99+
LL + }
100+
|
101+
102+
error: aborting due to 8 previous errors
87103

0 commit comments

Comments
 (0)