File tree Expand file tree Collapse file tree 4 files changed +149
-31
lines changed Expand file tree Collapse file tree 4 files changed +149
-31
lines changed Original file line number Diff line number Diff line change 11use clippy_utils:: diagnostics:: span_lint_and_then;
2- use clippy_utils:: path_to_local;
32use clippy_utils:: source:: snippet;
43use clippy_utils:: visitors:: { for_each_expr, is_local_used} ;
4+ use clippy_utils:: { in_constant, path_to_local} ;
55use rustc_ast:: { BorrowKind , LitKind } ;
66use rustc_errors:: Applicability ;
77use 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 ( )
Original file line number Diff line number Diff line change 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)]
1818struct FloatWrapper(f32);
19+
1920fn 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+ }
Original file line number Diff line number Diff line change 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 ) ]
1818struct FloatWrapper ( f32 ) ;
19+
1920fn 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+ }
You can’t perform that action at this time.
0 commit comments