|
| 1 | +// This software is available under the Apache-2.0 license. |
| 2 | +// See https://www.apache.org/licenses/LICENSE-2.0.txt for full text. |
| 3 | +// |
| 4 | +// Copyright (c) 2024, Gluu, Inc. |
| 5 | + |
| 6 | +#![feature(rustc_private)] |
| 7 | +#![warn(unused_extern_crates)] |
| 8 | + |
| 9 | +extern crate rustc_ast; |
| 10 | +extern crate rustc_errors; |
| 11 | +extern crate rustc_hir; |
| 12 | +extern crate rustc_span; |
| 13 | + |
| 14 | +use clippy_utils::{ |
| 15 | + diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then}, |
| 16 | + source::snippet, |
| 17 | + sym, |
| 18 | +}; |
| 19 | +use rustc_ast::LitKind::Str; |
| 20 | +use rustc_errors::Applicability; |
| 21 | +use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind, def::Res::Local}; |
| 22 | +use rustc_lint::{LateContext, LateLintPass}; |
| 23 | +use rustc_span::{Symbol, source_map::Spanned}; |
| 24 | + |
| 25 | +dylint_linting::declare_late_lint! { |
| 26 | + /// ### What it does |
| 27 | + /// Checks for `EntityUid::from_str(&format!(...))` pattern which is explicitly |
| 28 | + /// warned against in the Cedar documentation for performance reasons. |
| 29 | + /// |
| 30 | + /// ### Why is this bad? |
| 31 | + /// The Cedar crate documentation specifically warns that using `format!()` |
| 32 | + /// with `EntityUid::from_str` is inefficient. Use string literals instead. |
| 33 | + /// |
| 34 | + /// ### Example |
| 35 | + /// |
| 36 | + /// ```rust |
| 37 | + /// EntityUid::from_str(&format!("{}::\"{}\"", entity_type, id)) |
| 38 | + /// ``` |
| 39 | + /// |
| 40 | + /// Use instead: |
| 41 | + /// |
| 42 | + /// ```rust |
| 43 | + /// let entity_type_name = EntityTypeName::from_str(entity_type).unwrap(); |
| 44 | + /// let entity_id = EntityId::from_str(id).unwrap_or_else(|e| match e {}); |
| 45 | + /// |
| 46 | + /// EntityUid::from_type_name_and_id(entity_type_name, entity_id) |
| 47 | + /// ``` |
| 48 | + pub BAD_STRING_CONCATENATION, |
| 49 | + Warn, |
| 50 | + "using EntityUid::from_str with format!() is inefficient" |
| 51 | +} |
| 52 | + |
| 53 | +impl<'tcx> LateLintPass<'tcx> for BadStringConcatenation { |
| 54 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 55 | + // This checks if the expression kind is a function call extracting |
| 56 | + // the function itself and its arguments |
| 57 | + if let ExprKind::Call(callee, [arg]) = &expr.kind |
| 58 | + // Checks the function's kind is a path to definition e.g., Vec::new or EntityUid::from_str |
| 59 | + && let ExprKind::Path(QPath::TypeRelative(ty, segment)) = &callee.kind |
| 60 | + // Checks the function's type is a path to a type e.g., Vec<T> or EntityUid |
| 61 | + && let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind |
| 62 | + // Checks for the type path segments identifier is `EntityUid` or not |
| 63 | + // e.g., cedar_policy::EntityUid::from_str will have 2 path segments cedar_policy and EntityUid |
| 64 | + && path.segments.last().is_some_and(|s| s.ident.name.as_str().eq("EntityUid")) |
| 65 | + // Checks if the function name is `from_str` or not |
| 66 | + && segment.ident.name == sym::from_str |
| 67 | + // Checks if the argument of `from_str` is a reference and extract the expression inside |
| 68 | + && let ExprKind::AddrOf(_, _, exp) = arg.kind |
| 69 | + { |
| 70 | + // Checks if the argument inside `from_str` is a function call or a definition |
| 71 | + match exp.kind { |
| 72 | + ExprKind::Path(QPath::Resolved(_, path)) => { |
| 73 | + if let Local(id) = path.res |
| 74 | + && let Node::Pat(pattern) = cx.tcx.hir_node(id) |
| 75 | + && let Node::LetStmt(parent) = cx.tcx.parent_hir_node(pattern.hir_id) |
| 76 | + && let Some(init) = parent.init |
| 77 | + && let ExprKind::Call(_, [arg]) = &init.kind |
| 78 | + && extract_format_args(arg).is_some() |
| 79 | + { |
| 80 | + span_lint_and_then( |
| 81 | + cx, |
| 82 | + BAD_STRING_CONCATENATION, |
| 83 | + expr.span, |
| 84 | + "using `EntityUid::from_str` with format! is inefficient", |
| 85 | + |diag| { |
| 86 | + diag.span_note(parent.span, "variable defined here"); |
| 87 | + }, |
| 88 | + ); |
| 89 | + } |
| 90 | + }, |
| 91 | + ExprKind::Call(_, [arg]) => { |
| 92 | + // Checks if the given expression is a format macro |
| 93 | + if let Some([format_arg]) = extract_format_args(arg) { |
| 94 | + if let Some(arg) = extract_string_literal(format_arg) { |
| 95 | + let ty_snippet = snippet(cx, ty.span, "EntityUid"); |
| 96 | + span_lint_and_sugg( |
| 97 | + cx, |
| 98 | + BAD_STRING_CONCATENATION, |
| 99 | + expr.span, |
| 100 | + "using `EntityUid::from_str` with format! is inefficient", |
| 101 | + "try this instead", |
| 102 | + format!("{ty_snippet}::from_str({arg:?})"), |
| 103 | + Applicability::MachineApplicable, |
| 104 | + ); |
| 105 | + } else { |
| 106 | + span_lint_and_help( |
| 107 | + cx, |
| 108 | + BAD_STRING_CONCATENATION, |
| 109 | + expr.span, |
| 110 | + "using `EntityUid::from_str` with format! is inefficient", |
| 111 | + None, |
| 112 | + "consider using `EntityUid::from_type_name_and_id` instead", |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + _ => {}, |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Checks if the given expression is a format macro or not and returns the arguments |
| 124 | +fn extract_format_args<'tcx>(expr: &Expr<'tcx>) -> Option<&'tcx [Expr<'tcx>]> { |
| 125 | + if let ExprKind::Block(block, _) = expr.kind |
| 126 | + && let Some(bexpr) = block.expr |
| 127 | + && let ExprKind::Call(_, args) = &bexpr.kind |
| 128 | + { |
| 129 | + return Some(args); |
| 130 | + } |
| 131 | + |
| 132 | + None |
| 133 | +} |
| 134 | + |
| 135 | +// Checks if the argument of format macro is a string literal and extract it |
| 136 | +fn extract_string_literal(expr: &Expr<'_>) -> Option<Symbol> { |
| 137 | + if let ExprKind::Call(_, [arg]) = expr.kind |
| 138 | + && let ExprKind::AddrOf(_, _, expr) = arg.kind |
| 139 | + && let ExprKind::Array([element]) = expr.kind |
| 140 | + && let ExprKind::Lit(Spanned { |
| 141 | + node: Str(item, _), .. |
| 142 | + }) = element.kind |
| 143 | + { |
| 144 | + return Some(item); |
| 145 | + } |
| 146 | + |
| 147 | + None |
| 148 | +} |
| 149 | + |
| 150 | +#[test] |
| 151 | +fn ui() { |
| 152 | + dylint_testing::ui_test(env!("CARGO_PKG_NAME"), "ui"); |
| 153 | +} |
0 commit comments