|
| 1 | +use rustc_hir::Expr; |
| 2 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 3 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 4 | +use rustc_span::{self, Span, Symbol}; |
| 5 | + |
| 6 | +use crate::ctxt::AnalysisCtxt; |
| 7 | + |
| 8 | +declare_tool_lint! { |
| 9 | + /// The `c_str_literal` lint detects when the kernel `c_str!` macro is used on a string literal. |
| 10 | + pub klint::C_STR_LITERAL, |
| 11 | + Warn, |
| 12 | + "`c_str!` used on a string literal" |
| 13 | +} |
| 14 | + |
| 15 | +pub struct CStrLiteralLint<'tcx> { |
| 16 | + pub cx: &'tcx AnalysisCtxt<'tcx>, |
| 17 | +} |
| 18 | + |
| 19 | +impl_lint_pass!(CStrLiteralLint<'_> => [C_STR_LITERAL]); |
| 20 | + |
| 21 | +#[derive(Diagnostic)] |
| 22 | +#[diag("`{$macro_name}!` is used on a literal")] |
| 23 | +struct CStrLiteral { |
| 24 | + #[primary_span] |
| 25 | + #[suggestion( |
| 26 | + "use C-string literals instead", |
| 27 | + code = "c{arg}", |
| 28 | + applicability = "machine-applicable" |
| 29 | + )] |
| 30 | + pub span: Span, |
| 31 | + pub macro_name: Symbol, |
| 32 | + pub arg: String, |
| 33 | +} |
| 34 | + |
| 35 | +impl<'tcx> CStrLiteralLint<'tcx> { |
| 36 | + fn extract_arg(&self, span: Span) -> Option<String> { |
| 37 | + let source = self.cx.sess.source_map().span_to_snippet(span).ok()?; |
| 38 | + |
| 39 | + let arg = source.split_once('!')?.1; |
| 40 | + if arg.len() <= 2 { |
| 41 | + return None; |
| 42 | + } |
| 43 | + |
| 44 | + Some(arg[1..arg.len() - 1].trim().to_owned()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<'tcx> LateLintPass<'tcx> for CStrLiteralLint<'tcx> { |
| 49 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 50 | + // If would be ideal if we can check before macro expansion. However, pre_expansion_lint is |
| 51 | + // not recommended because the lint level infrastructure if not yet ready, and also it does |
| 52 | + // not have information of the name resolution available so we cannot precisely determine if |
| 53 | + // the macro is something that we want to lint now. |
| 54 | + // |
| 55 | + // So, as an alternative strategy, try to backtrace macros and find an expression that is expanded |
| 56 | + // from the `c_str!` macro. Once that found, use span information to recover the argument used to |
| 57 | + // call the macro. |
| 58 | + // |
| 59 | + // However, given that `c_str!` have used `concat!()` internally, there is no single span in the |
| 60 | + // expanded HIR that maps to the input argument... As a very crude approximation, use source map |
| 61 | + // to obtain the source and check based on that. This is also very hacky but at least lint level |
| 62 | + // and name resolution works as intended... |
| 63 | + |
| 64 | + // TODO: This check will replicated multiple times for each sub-expression of `c_str!`. |
| 65 | + // It might be good if we stop early and stop recursing into sub-expressions, although this is not |
| 66 | + // something that can be achieved with `LateLintPass`. |
| 67 | + let span = expr.span; |
| 68 | + let Some(expn_data) = span.macro_backtrace().next() else { |
| 69 | + return; |
| 70 | + }; |
| 71 | + |
| 72 | + let Some(c_str) = self.cx.get_klint_diagnostic_item(crate::symbol::c_str) else { |
| 73 | + return; |
| 74 | + }; |
| 75 | + |
| 76 | + if expn_data.macro_def_id != Some(c_str) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if let Some(arg) = self.extract_arg(expn_data.call_site) { |
| 81 | + if arg.starts_with('"') && arg.ends_with('"') { |
| 82 | + cx.emit_diag_lint( |
| 83 | + C_STR_LITERAL, |
| 84 | + CStrLiteral { |
| 85 | + span: expn_data.call_site, |
| 86 | + macro_name: self.cx.item_name(c_str), |
| 87 | + arg, |
| 88 | + }, |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments