forge_lint/sol/high/
incorrect_shift.rs
1use super::IncorrectShift;
2use crate::{
3 linter::{EarlyLintPass, LintContext},
4 sol::{Severity, SolLint},
5};
6use solar_ast::{BinOp, BinOpKind, Expr, ExprKind};
7
8declare_forge_lint!(
9 INCORRECT_SHIFT,
10 Severity::High,
11 "incorrect-shift",
12 "the order of args in a shift operation is incorrect"
13);
14
15impl<'ast> EarlyLintPass<'ast> for IncorrectShift {
16 fn check_expr(&mut self, ctx: &LintContext<'_>, expr: &'ast Expr<'ast>) {
17 if let ExprKind::Binary(
18 left_expr,
19 BinOp { kind: BinOpKind::Shl | BinOpKind::Shr, .. },
20 right_expr,
21 ) = &expr.kind
22 && contains_incorrect_shift(left_expr, right_expr)
23 {
24 ctx.emit(&INCORRECT_SHIFT, expr.span);
25 }
26 }
27}
28
29fn contains_incorrect_shift<'ast>(
33 left_expr: &'ast Expr<'ast>,
34 right_expr: &'ast Expr<'ast>,
35) -> bool {
36 let is_left_literal = matches!(left_expr.kind, ExprKind::Lit(..));
37 let is_right_not_literal = !matches!(right_expr.kind, ExprKind::Lit(..));
38
39 is_left_literal && is_right_not_literal
40}