-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95c62ff
commit 5d840b6
Showing
9 changed files
with
475 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use std::cmp::Ordering; | ||
|
||
use super::UNNECESSARY_MIN_MAX; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
|
||
use clippy_utils::consts::{constant, Constant}; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::{clip, int_bits, unsext}; | ||
use hir::Expr; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
|
||
use rustc_middle::ty; | ||
use rustc_span::Span; | ||
|
||
pub fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
name: &str, | ||
recv: &'tcx Expr<'_>, | ||
arg: &'tcx Expr<'_>, | ||
) { | ||
let typeck_results = cx.typeck_results(); | ||
if let (Some(left), Some(right)) = (constant(cx, typeck_results, recv), constant(cx, typeck_results, 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); | ||
} | ||
} | ||
|
||
fn lint(cx: &LateContext<'_>, expr: &Expr<'_>, name: &str, lhs: Span, rhs: Span, order: Ordering) { | ||
let cmp_str = if (name == "min" && order.is_ge()) || (name == "max" && order.is_le()) { | ||
"smaller" | ||
} else { | ||
"greater" | ||
}; | ||
|
||
let suggested_value = if (name == "min" && order.is_ge()) || (name == "max" && order.is_le()) { | ||
snippet(cx, rhs, "..") | ||
} else { | ||
snippet(cx, lhs, "..") | ||
}; | ||
|
||
let msg = format!( | ||
"`{}` is never {} than `{}` and has therefore no effect", | ||
snippet(cx, lhs, ".."), | ||
cmp_str, | ||
snippet(cx, rhs, "..") | ||
); | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_MIN_MAX, | ||
expr.span, | ||
&msg, | ||
"try", | ||
suggested_value.to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Extrema { | ||
Minimum, | ||
Maximum, | ||
} | ||
fn detect_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Extrema> { | ||
let ty = cx.typeck_results().expr_ty(expr); | ||
|
||
let cv = constant(cx, cx.typeck_results(), expr)?; | ||
|
||
match (ty.kind(), cv) { | ||
(&ty::Uint(_), Constant::Int(0)) => Some(Extrema::Minimum), | ||
(&ty::Int(ity), Constant::Int(i)) if i == unsext(cx.tcx, i128::MIN >> (128 - int_bits(cx.tcx, ity)), ity) => { | ||
Some(Extrema::Minimum) | ||
}, | ||
|
||
(&ty::Int(ity), Constant::Int(i)) if i == unsext(cx.tcx, i128::MAX >> (128 - int_bits(cx.tcx, ity)), ity) => { | ||
Some(Extrema::Maximum) | ||
}, | ||
(&ty::Uint(uty), Constant::Int(i)) if i == clip(cx.tcx, u128::MAX, uty) => Some(Extrema::Maximum), | ||
|
||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.