Skip to content

Commit

Permalink
Auto merge of #13235 - kyoto7250:fix_issue_13191, r=llogiq
Browse files Browse the repository at this point in the history
Use `is_diagnostic_item` for checking a def_id in `unnecessary_min_or_max`.

close #13191
This PR fixes the false positives in `unnecessary_min_or_max `.
We should use `is_diagnostic_item` for checking def_ids in this lint.

----

changelog: fix false positive in `unnecessary_min_or_max `.
  • Loading branch information
bors committed Aug 28, 2024
2 parents 9e260ff + c2ed04b commit 04bded5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
45 changes: 24 additions & 21 deletions clippy_lints/src/methods/unnecessary_min_or_max.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::cmp::Ordering;

use super::UNNECESSARY_MIN_OR_MAX;
use clippy_utils::diagnostics::span_lint_and_sugg;

use clippy_utils::consts::{ConstEvalCtxt, Constant, ConstantSource, FullInt};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;

use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::Span;
use rustc_span::{sym, Span};

pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
Expand All @@ -21,26 +20,30 @@ pub(super) fn check<'tcx>(
) {
let typeck_results = cx.typeck_results();
let ecx = ConstEvalCtxt::with_env(cx.tcx, cx.param_env, typeck_results);
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)
if let Some(id) = typeck_results.type_dependent_def_id(expr.hir_id)
&& (cx.tcx.is_diagnostic_item(sym::cmp_ord_min, id) || cx.tcx.is_diagnostic_item(sym::cmp_ord_max, id))
{
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
return;
};
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)
{
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
return;
};

lint(cx, expr, name, recv.span, arg.span, ord);
} else if let Some(extrema) = detect_extrema(cx, recv) {
let ord = match extrema {
Extrema::Minimum => Ordering::Less,
Extrema::Maximum => Ordering::Greater,
};
lint(cx, expr, name, recv.span, arg.span, ord);
} else if let Some(extrema) = detect_extrema(cx, arg) {
let ord = match extrema {
Extrema::Minimum => Ordering::Greater,
Extrema::Maximum => Ordering::Less,
};
lint(cx, expr, name, recv.span, arg.span, ord);
lint(cx, expr, name, recv.span, arg.span, ord);
} else if let Some(extrema) = detect_extrema(cx, recv) {
let ord = match extrema {
Extrema::Minimum => Ordering::Less,
Extrema::Maximum => Ordering::Greater,
};
lint(cx, expr, name, recv.span, arg.span, ord);
} else if let Some(extrema) = detect_extrema(cx, arg) {
let ord = match extrema {
Extrema::Minimum => Ordering::Greater,
Extrema::Maximum => Ordering::Less,
};
lint(cx, expr, name, recv.span, arg.span, ord);
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/ui/unnecessary_min_or_max.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,32 @@ fn random_u32() -> u32 {
// random number generator
0
}

struct Issue13191 {
min: u16,
max: u16,
}

impl Issue13191 {
fn new() -> Self {
Self { min: 0, max: 0 }
}

fn min(mut self, value: u16) -> Self {
self.min = value;
self
}

fn max(mut self, value: u16) -> Self {
self.max = value;
self
}
}

fn issue_13191() {
// should not fixed
Issue13191::new().min(0);

// should not fixed
Issue13191::new().max(0);
}
29 changes: 29 additions & 0 deletions tests/ui/unnecessary_min_or_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,32 @@ fn random_u32() -> u32 {
// random number generator
0
}

struct Issue13191 {
min: u16,
max: u16,
}

impl Issue13191 {
fn new() -> Self {
Self { min: 0, max: 0 }
}

fn min(mut self, value: u16) -> Self {
self.min = value;
self
}

fn max(mut self, value: u16) -> Self {
self.max = value;
self
}
}

fn issue_13191() {
// should not fixed
Issue13191::new().min(0);

// should not fixed
Issue13191::new().max(0);
}

0 comments on commit 04bded5

Please sign in to comment.