11use rustc_hir:: { BinOpKind , Expr , ExprKind } ;
2- use rustc_lint:: { LateContext , LateLintPass } ;
2+ use rustc_lint:: LateContext ;
33use rustc_middle:: ty;
4- use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
54
65use clippy_utils:: comparisons:: { normalize_comparison, Rel } ;
76use clippy_utils:: consts:: { constant, Constant } ;
@@ -10,73 +9,41 @@ use clippy_utils::source::snippet;
109use clippy_utils:: ty:: is_isize_or_usize;
1110use clippy_utils:: { clip, int_bits, unsext} ;
1211
13- declare_clippy_lint ! {
14- /// ### What it does
15- /// Checks for comparisons where one side of the relation is
16- /// either the minimum or maximum value for its type and warns if it involves a
17- /// case that is always true or always false. Only integer and boolean types are
18- /// checked.
19- ///
20- /// ### Why is this bad?
21- /// An expression like `min <= x` may misleadingly imply
22- /// that it is possible for `x` to be less than the minimum. Expressions like
23- /// `max < x` are probably mistakes.
24- ///
25- /// ### Known problems
26- /// For `usize` the size of the current compile target will
27- /// be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such
28- /// a comparison to detect target pointer width will trigger this lint. One can
29- /// use `mem::sizeof` and compare its value or conditional compilation
30- /// attributes
31- /// like `#[cfg(target_pointer_width = "64")] ..` instead.
32- ///
33- /// ### Example
34- /// ```rust
35- /// let vec: Vec<isize> = Vec::new();
36- /// if vec.len() <= 0 {}
37- /// if 100 > i32::MAX {}
38- /// ```
39- #[ clippy:: version = "pre 1.29.0" ]
40- pub ABSURD_EXTREME_COMPARISONS ,
41- correctness,
42- "a comparison with a maximum or minimum value that is always true or false"
43- }
12+ use super :: ABSURD_EXTREME_COMPARISONS ;
4413
45- declare_lint_pass ! ( AbsurdExtremeComparisons => [ ABSURD_EXTREME_COMPARISONS ] ) ;
46-
47- impl < ' tcx > LateLintPass < ' tcx > for AbsurdExtremeComparisons {
48- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
49- if let ExprKind :: Binary ( ref cmp, lhs, rhs) = expr. kind {
50- if let Some ( ( culprit, result) ) = detect_absurd_comparison ( cx, cmp. node , lhs, rhs) {
51- if !expr. span . from_expansion ( ) {
52- let msg = "this comparison involving the minimum or maximum element for this \
53- type contains a case that is always true or always false";
54-
55- let conclusion = match result {
56- AbsurdComparisonResult :: AlwaysFalse => "this comparison is always false" . to_owned ( ) ,
57- AbsurdComparisonResult :: AlwaysTrue => "this comparison is always true" . to_owned ( ) ,
58- AbsurdComparisonResult :: InequalityImpossible => format ! (
59- "the case where the two sides are not equal never occurs, consider using `{} == {}` \
60- instead",
61- snippet( cx, lhs. span, "lhs" ) ,
62- snippet( cx, rhs. span, "rhs" )
63- ) ,
64- } ;
65-
66- let help = format ! (
67- "because `{}` is the {} value for this type, {}" ,
68- snippet( cx, culprit. expr. span, "x" ) ,
69- match culprit. which {
70- ExtremeType :: Minimum => "minimum" ,
71- ExtremeType :: Maximum => "maximum" ,
72- } ,
73- conclusion
74- ) ;
75-
76- span_lint_and_help ( cx, ABSURD_EXTREME_COMPARISONS , expr. span , msg, None , & help) ;
77- }
78- }
79- }
14+ pub ( super ) fn check < ' tcx > (
15+ cx : & LateContext < ' tcx > ,
16+ expr : & ' tcx Expr < ' _ > ,
17+ op : BinOpKind ,
18+ lhs : & ' tcx Expr < ' _ > ,
19+ rhs : & ' tcx Expr < ' _ > ,
20+ ) {
21+ if let Some ( ( culprit, result) ) = detect_absurd_comparison ( cx, op, lhs, rhs) {
22+ let msg = "this comparison involving the minimum or maximum element for this \
23+ type contains a case that is always true or always false";
24+
25+ let conclusion = match result {
26+ AbsurdComparisonResult :: AlwaysFalse => "this comparison is always false" . to_owned ( ) ,
27+ AbsurdComparisonResult :: AlwaysTrue => "this comparison is always true" . to_owned ( ) ,
28+ AbsurdComparisonResult :: InequalityImpossible => format ! (
29+ "the case where the two sides are not equal never occurs, consider using `{} == {}` \
30+ instead",
31+ snippet( cx, lhs. span, "lhs" ) ,
32+ snippet( cx, rhs. span, "rhs" )
33+ ) ,
34+ } ;
35+
36+ let help = format ! (
37+ "because `{}` is the {} value for this type, {}" ,
38+ snippet( cx, culprit. expr. span, "x" ) ,
39+ match culprit. which {
40+ ExtremeType :: Minimum => "minimum" ,
41+ ExtremeType :: Maximum => "maximum" ,
42+ } ,
43+ conclusion
44+ ) ;
45+
46+ span_lint_and_help ( cx, ABSURD_EXTREME_COMPARISONS , expr. span , msg, None , & help) ;
8047 }
8148}
8249
0 commit comments