Skip to content

Commit a42b773

Browse files
committed
fix: redundant_pattern_matching misses ) in suggestion span
1 parent da2a220 commit a42b773

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

clippy_lints/src/matches/redundant_pattern_match.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::REDUNDANT_PATTERN_MATCHING;
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
4-
use clippy_utils::source::walk_span_to_context;
54
use clippy_utils::sugg::{Sugg, make_unop};
65
use clippy_utils::ty::needs_ordered_drop;
76
use clippy_utils::visitors::{any_temporaries_need_ordered_drop, for_each_expr_without_closures};
@@ -25,7 +24,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
2524
..
2625
}) = higher::WhileLet::hir(expr)
2726
{
28-
find_method_sugg_for_if_let(cx, expr, let_pat, let_expr, "while", false);
27+
find_method_sugg_for_if_let(cx, expr, let_pat, let_expr, "while", false, let_span);
2928
find_if_let_true(cx, let_pat, let_expr, let_span);
3029
}
3130
}
@@ -39,7 +38,7 @@ pub(super) fn check_if_let<'tcx>(
3938
let_span: Span,
4039
) {
4140
find_if_let_true(cx, pat, scrutinee, let_span);
42-
find_method_sugg_for_if_let(cx, expr, pat, scrutinee, "if", has_else);
41+
find_method_sugg_for_if_let(cx, expr, pat, scrutinee, "if", has_else, let_span);
4342
}
4443

4544
/// Looks for:
@@ -182,6 +181,7 @@ fn find_method_sugg_for_if_let<'tcx>(
182181
let_expr: &'tcx Expr<'_>,
183182
keyword: &'static str,
184183
has_else: bool,
184+
let_span: Span,
185185
) {
186186
// also look inside refs
187187
// if we have &None for example, peel it so we can detect "if let None = x"
@@ -239,15 +239,9 @@ fn find_method_sugg_for_if_let<'tcx>(
239239
let expr_span = expr.span;
240240
let ctxt = expr.span.ctxt();
241241

242-
// if/while let ... = ... { ... }
243-
// ^^^
244-
let Some(res_span) = walk_span_to_context(result_expr.span.source_callsite(), ctxt) else {
245-
return;
246-
};
247-
248242
// if/while let ... = ... { ... }
249243
// ^^^^^^^^^^^^^^^^^^^^^^
250-
let span = expr_span.until(res_span.shrink_to_hi());
244+
let span = expr_span.until(let_span.shrink_to_hi());
251245

252246
let mut app = if needs_drop {
253247
Applicability::MaybeIncorrect

tests/ui/redundant_pattern_matching_option.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,16 @@ fn issue13902() {
166166
//~^ redundant_pattern_matching
167167
}
168168
}
169+
170+
fn issue14989() {
171+
macro_rules! x {
172+
() => {
173+
None::<i32>
174+
};
175+
}
176+
177+
if x! {}.is_some() {};
178+
//~^ redundant_pattern_matching
179+
while x! {}.is_some() {}
180+
//~^ redundant_pattern_matching
181+
}

tests/ui/redundant_pattern_matching_option.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,16 @@ fn issue13902() {
202202
//~^ redundant_pattern_matching
203203
}
204204
}
205+
206+
fn issue14989() {
207+
macro_rules! x {
208+
() => {
209+
None::<i32>
210+
};
211+
}
212+
213+
if let Some(_) = (x! {}) {};
214+
//~^ redundant_pattern_matching
215+
while let Some(_) = (x! {}) {}
216+
//~^ redundant_pattern_matching
217+
}

tests/ui/redundant_pattern_matching_option.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,17 @@ error: redundant pattern matching, consider using `is_none()`
224224
LL | let _ = matches!(*p, None);
225225
| ^^^^^^^^^^^^^^^^^^ help: try: `(*p).is_none()`
226226

227-
error: aborting due to 31 previous errors
227+
error: redundant pattern matching, consider using `is_some()`
228+
--> tests/ui/redundant_pattern_matching_option.rs:213:12
229+
|
230+
LL | if let Some(_) = (x! {}) {};
231+
| -------^^^^^^^---------- help: try: `if x! {}.is_some()`
232+
233+
error: redundant pattern matching, consider using `is_some()`
234+
--> tests/ui/redundant_pattern_matching_option.rs:215:15
235+
|
236+
LL | while let Some(_) = (x! {}) {}
237+
| ----------^^^^^^^---------- help: try: `while x! {}.is_some()`
238+
239+
error: aborting due to 33 previous errors
228240

0 commit comments

Comments
 (0)