Skip to content

Commit

Permalink
FIX(12243): redundant_guard
Browse files Browse the repository at this point in the history
A warning is now suppressed when "<str_va> if <str_var>.is_empty" is used in a constant function.

FIX: instead of clippy_util::in_const

FIX: Merged `redundant_guards_const_fn.rs` into `redundant_guards.rs`.
  • Loading branch information
not-elm committed Feb 25, 2024
1 parent a2c1d56 commit 5a63cd8
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 5a63cd8

Please sign in to comment.