52 lines
1.8 KiB
Rust
52 lines
1.8 KiB
Rust
|
use rustc_hir::{Expr, ExprKind};
|
||
|
use rustc_lint::{LateContext, LateLintPass};
|
||
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||
|
|
||
|
mod absurd_extreme_comparisons;
|
||
|
|
||
|
declare_clippy_lint! {
|
||
|
/// ### What it does
|
||
|
/// Checks for comparisons where one side of the relation is
|
||
|
/// either the minimum or maximum value for its type and warns if it involves a
|
||
|
/// case that is always true or always false. Only integer and boolean types are
|
||
|
/// checked.
|
||
|
///
|
||
|
/// ### Why is this bad?
|
||
|
/// An expression like `min <= x` may misleadingly imply
|
||
|
/// that it is possible for `x` to be less than the minimum. Expressions like
|
||
|
/// `max < x` are probably mistakes.
|
||
|
///
|
||
|
/// ### Known problems
|
||
|
/// For `usize` the size of the current compile target will
|
||
|
/// be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such
|
||
|
/// a comparison to detect target pointer width will trigger this lint. One can
|
||
|
/// use `mem::sizeof` and compare its value or conditional compilation
|
||
|
/// attributes
|
||
|
/// like `#[cfg(target_pointer_width = "64")] ..` instead.
|
||
|
///
|
||
|
/// ### Example
|
||
|
/// ```rust
|
||
|
/// let vec: Vec<isize> = Vec::new();
|
||
|
/// if vec.len() <= 0 {}
|
||
|
/// if 100 > i32::MAX {}
|
||
|
/// ```
|
||
|
#[clippy::version = "pre 1.29.0"]
|
||
|
pub ABSURD_EXTREME_COMPARISONS,
|
||
|
correctness,
|
||
|
"a comparison with a maximum or minimum value that is always true or false"
|
||
|
}
|
||
|
|
||
|
pub struct Operators;
|
||
|
impl_lint_pass!(Operators => [
|
||
|
ABSURD_EXTREME_COMPARISONS,
|
||
|
]);
|
||
|
impl<'tcx> LateLintPass<'tcx> for Operators {
|
||
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
||
|
if let ExprKind::Binary(op, lhs, rhs) = e.kind {
|
||
|
if !e.span.from_expansion() {
|
||
|
absurd_extreme_comparisons::check(cx, e, op.node, lhs, rhs);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|