Skip to content

Commit c469cb0

Browse files
committed
Auto merge of rust-lang#12336 - not-elm:fix/issue-12243, r=y21
FIX(12243): redundant_guards Fixed rust-lang#12243 changelog: Fix[`redundant_guards`] I have made a correction so that no warning does appear when y.is_empty() is used within a constant function as follows. ```rust pub const fn const_fn(x: &str) { match x { // Shouldn't lint. y if y.is_empty() => {}, _ => {}, } } ```
2 parents 76e4864 + 5a63cd8 commit c469cb0

File tree

4 files changed

+149
-31
lines changed

4 files changed

+149
-31
lines changed

clippy_lints/src/matches/redundant_guards.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::path_to_local;
32
use clippy_utils::source::snippet;
43
use clippy_utils::visitors::{for_each_expr, is_local_used};
4+
use clippy_utils::{in_constant, path_to_local};
55
use rustc_ast::{BorrowKind, LitKind};
66
use rustc_errors::Applicability;
77
use rustc_hir::def::{DefKind, Res};
@@ -123,7 +123,7 @@ fn check_method_calls<'tcx>(
123123
// `s if s.is_empty()` becomes ""
124124
// `arr if arr.is_empty()` becomes []
125125

126-
if ty.is_str() {
126+
if ty.is_str() && !in_constant(cx, if_expr.hir_id) {
127127
r#""""#.into()
128128
} else if slice_like {
129129
"[]".into()

tests/ui/redundant_guards.fixed

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macros.rs
22
#![feature(if_let_guard)]
3-
#![allow(clippy::no_effect, unused)]
3+
#![allow(clippy::no_effect, unused, clippy::single_match)]
44
#![warn(clippy::redundant_guards)]
55

66
#[macro_use]
@@ -16,6 +16,7 @@ struct C(u32, u32);
1616

1717
#[derive(PartialEq)]
1818
struct FloatWrapper(f32);
19+
1920
fn issue11304() {
2021
match 0.1 {
2122
x if x == 0.0 => todo!(),
@@ -258,3 +259,49 @@ fn issue11807() {
258259
_ => {},
259260
}
260261
}
262+
263+
mod issue12243 {
264+
pub const fn const_fn(x: &str) {
265+
match x {
266+
// Shouldn't lint.
267+
y if y.is_empty() => {},
268+
_ => {},
269+
}
270+
}
271+
272+
pub fn non_const_fn(x: &str) {
273+
match x {
274+
"" => {},
275+
//~^ ERROR: redundant guard
276+
_ => {},
277+
}
278+
}
279+
280+
struct Bar;
281+
282+
impl Bar {
283+
pub const fn const_bar(x: &str) {
284+
match x {
285+
// Shouldn't lint.
286+
y if y.is_empty() => {},
287+
_ => {},
288+
}
289+
}
290+
291+
pub fn non_const_bar(x: &str) {
292+
match x {
293+
"" => {},
294+
//~^ ERROR: redundant guard
295+
_ => {},
296+
}
297+
}
298+
}
299+
300+
static FOO: () = {
301+
match "" {
302+
// Shouldn't lint.
303+
x if x.is_empty() => {},
304+
_ => {},
305+
}
306+
};
307+
}

tests/ui/redundant_guards.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macros.rs
22
#![feature(if_let_guard)]
3-
#![allow(clippy::no_effect, unused)]
3+
#![allow(clippy::no_effect, unused, clippy::single_match)]
44
#![warn(clippy::redundant_guards)]
55

66
#[macro_use]
@@ -16,6 +16,7 @@ struct C(u32, u32);
1616

1717
#[derive(PartialEq)]
1818
struct FloatWrapper(f32);
19+
1920
fn issue11304() {
2021
match 0.1 {
2122
x if x == 0.0 => todo!(),
@@ -258,3 +259,49 @@ fn issue11807() {
258259
_ => {},
259260
}
260261
}
262+
263+
mod issue12243 {
264+
pub const fn const_fn(x: &str) {
265+
match x {
266+
// Shouldn't lint.
267+
y if y.is_empty() => {},
268+
_ => {},
269+
}
270+
}
271+
272+
pub fn non_const_fn(x: &str) {
273+
match x {
274+
y if y.is_empty() => {},
275+
//~^ ERROR: redundant guard
276+
_ => {},
277+
}
278+
}
279+
280+
struct Bar;
281+
282+
impl Bar {
283+
pub const fn const_bar(x: &str) {
284+
match x {
285+
// Shouldn't lint.
286+
y if y.is_empty() => {},
287+
_ => {},
288+
}
289+
}
290+
291+
pub fn non_const_bar(x: &str) {
292+
match x {
293+
y if y.is_empty() => {},
294+
//~^ ERROR: redundant guard
295+
_ => {},
296+
}
297+
}
298+
}
299+
300+
static FOO: () = {
301+
match "" {
302+
// Shouldn't lint.
303+
x if x.is_empty() => {},
304+
_ => {},
305+
}
306+
};
307+
}

0 commit comments

Comments
 (0)