forge_lint/sol/gas/
keccak.rs

1use super::AsmKeccak256;
2use crate::{
3    linter::{EarlyLintPass, LintContext},
4    sol::{Severity, SolLint},
5};
6use solar_ast::{CallArgsKind, Expr, ExprKind};
7use solar_interface::kw;
8
9declare_forge_lint!(
10    ASM_KECCAK256,
11    Severity::Gas,
12    "asm-keccak256",
13    "hash using inline assembly to save gas"
14);
15
16impl<'ast> EarlyLintPass<'ast> for AsmKeccak256 {
17    fn check_expr(&mut self, ctx: &LintContext<'_>, expr: &'ast Expr<'ast>) {
18        if let ExprKind::Call(expr, args) = &expr.kind
19            && let ExprKind::Ident(ident) = &expr.kind
20            && ident.name == kw::Keccak256
21        {
22            // Do not flag when hashing a single literal, as the compiler should optimize it
23            if let CallArgsKind::Unnamed(ref exprs) = args.kind
24                && exprs.len() == 1
25                && let ExprKind::Lit(_, _) = exprs[0].kind
26            {
27                return;
28            }
29            ctx.emit(&ASM_KECCAK256, expr.span);
30        }
31    }
32}