|
| 1 | +use clippy_config::msrvs::{self, Msrv}; |
| 2 | +use clippy_config::Conf; |
1 | 3 | use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; |
2 | 4 | use clippy_utils::source::snippet_opt; |
3 | | -use clippy_utils::{higher, is_integer_literal, path_to_local, peel_blocks, peel_blocks_with_stmt, SpanlessEq}; |
| 5 | +use clippy_utils::{ |
| 6 | + higher, in_constant, is_integer_literal, path_to_local, peel_blocks, peel_blocks_with_stmt, SpanlessEq, |
| 7 | +}; |
4 | 8 | use rustc_ast::ast::LitKind; |
5 | 9 | use rustc_data_structures::packed::Pu128; |
6 | 10 | use rustc_errors::Applicability; |
7 | 11 | use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, HirId, QPath}; |
8 | 12 | use rustc_lint::{LateContext, LateLintPass}; |
9 | | -use rustc_session::declare_lint_pass; |
| 13 | +use rustc_session::impl_lint_pass; |
10 | 14 | use rustc_span::Span; |
11 | 15 |
|
12 | 16 | declare_clippy_lint! { |
@@ -70,13 +74,28 @@ declare_clippy_lint! { |
70 | 74 | "Check if a variable is smaller than another one and still subtract from it even if smaller" |
71 | 75 | } |
72 | 76 |
|
73 | | -declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]); |
| 77 | +pub struct ImplicitSaturatingSub { |
| 78 | + msrv: Msrv, |
| 79 | +} |
| 80 | + |
| 81 | +impl_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]); |
| 82 | + |
| 83 | +impl ImplicitSaturatingSub { |
| 84 | + pub fn new(conf: &'static Conf) -> Self { |
| 85 | + Self { |
| 86 | + msrv: conf.msrv.clone(), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
74 | 90 |
|
75 | 91 | impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub { |
76 | 92 | fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
77 | 93 | if expr.span.from_expansion() { |
78 | 94 | return; |
79 | 95 | } |
| 96 | + if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::SATURATING_SUB_CONST) { |
| 97 | + return; |
| 98 | + } |
80 | 99 | if let Some(higher::If { cond, then, r#else: None }) = higher::If::hir(expr) |
81 | 100 |
|
82 | 101 | // Check if the conditional expression is a binary operation |
|
0 commit comments