Skip to content

Commit

Permalink
Auto merge of #12336 - not-elm:fix/issue-12243, r=y21
Browse files Browse the repository at this point in the history
FIX(12243): redundant_guards

Fixed #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() => {},
        _ => {},
    }
}
```
  • Loading branch information
bors committed Feb 25, 2024
2 parents 76e4864 + 5a63cd8 commit c469cb0
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 31 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/matches/redundant_guards.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::path_to_local;
use clippy_utils::source::snippet;
use clippy_utils::visitors::{for_each_expr, is_local_used};
use clippy_utils::{in_constant, path_to_local};
use rustc_ast::{BorrowKind, LitKind};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -123,7 +123,7 @@ fn check_method_calls<'tcx>(
// `s if s.is_empty()` becomes ""
// `arr if arr.is_empty()` becomes []

if ty.is_str() {
if ty.is_str() && !in_constant(cx, if_expr.hir_id) {
r#""""#.into()
} else if slice_like {
"[]".into()
Expand Down
49 changes: 48 additions & 1 deletion tests/ui/redundant_guards.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macros.rs
#![feature(if_let_guard)]
#![allow(clippy::no_effect, unused)]
#![allow(clippy::no_effect, unused, clippy::single_match)]
#![warn(clippy::redundant_guards)]

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

#[derive(PartialEq)]
struct FloatWrapper(f32);

fn issue11304() {
match 0.1 {
x if x == 0.0 => todo!(),
Expand Down Expand Up @@ -258,3 +259,49 @@ fn issue11807() {
_ => {},
}
}

mod issue12243 {
pub const fn const_fn(x: &str) {
match x {
// Shouldn't lint.
y if y.is_empty() => {},
_ => {},
}
}

pub fn non_const_fn(x: &str) {
match x {
"" => {},
//~^ ERROR: redundant guard
_ => {},
}
}

struct Bar;

impl Bar {
pub const fn const_bar(x: &str) {
match x {
// Shouldn't lint.
y if y.is_empty() => {},
_ => {},
}
}

pub fn non_const_bar(x: &str) {
match x {
"" => {},
//~^ ERROR: redundant guard
_ => {},
}
}
}

static FOO: () = {
match "" {
// Shouldn't lint.
x if x.is_empty() => {},
_ => {},
}
};
}
49 changes: 48 additions & 1 deletion tests/ui/redundant_guards.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@aux-build:proc_macros.rs
#![feature(if_let_guard)]
#![allow(clippy::no_effect, unused)]
#![allow(clippy::no_effect, unused, clippy::single_match)]
#![warn(clippy::redundant_guards)]

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

#[derive(PartialEq)]
struct FloatWrapper(f32);

fn issue11304() {
match 0.1 {
x if x == 0.0 => todo!(),
Expand Down Expand Up @@ -258,3 +259,49 @@ fn issue11807() {
_ => {},
}
}

mod issue12243 {
pub const fn const_fn(x: &str) {
match x {
// Shouldn't lint.
y if y.is_empty() => {},
_ => {},
}
}

pub fn non_const_fn(x: &str) {
match x {
y if y.is_empty() => {},
//~^ ERROR: redundant guard
_ => {},
}
}

struct Bar;

impl Bar {
pub const fn const_bar(x: &str) {
match x {
// Shouldn't lint.
y if y.is_empty() => {},
_ => {},
}
}

pub fn non_const_bar(x: &str) {
match x {
y if y.is_empty() => {},
//~^ ERROR: redundant guard
_ => {},
}
}
}

static FOO: () = {
match "" {
// Shouldn't lint.
x if x.is_empty() => {},
_ => {},
}
};
}
Loading

0 comments on commit c469cb0

Please sign in to comment.