|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::is_from_proc_macro; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::def_id::LocalDefId; |
| 6 | +use rustc_hir::intravisit::FnKind; |
| 7 | +use rustc_hir::{Body, FnDecl, FnHeader, Item, ItemKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | +use rustc_span::Span; |
| 12 | +use rustc_target::spec::abi::Abi; |
| 13 | + |
| 14 | +const LINT_MSG: &str = "`extern` missing explicit ABI"; |
| 15 | +const LINT_HELP_MSG: &str = "consider using"; |
| 16 | + |
| 17 | +const EXTERN: &str = "extern"; |
| 18 | +const FN: &str = "fn"; |
| 19 | +const OPEN_BRACE: &str = "{"; |
| 20 | +const ABI: &str = r#""C""#; |
| 21 | + |
| 22 | +declare_clippy_lint! { |
| 23 | + /// ### What it does |
| 24 | + /// Checks for usage of `extern` without an explicit ABI. |
| 25 | + /// |
| 26 | + /// ### Why is this bad? |
| 27 | + /// Explicitly declaring the ABI is the recommended convention. See: |
| 28 | + /// [Rust Style Guide - `extern` items](https://doc.rust-lang.org/nightly/style-guide/items.html#extern-items) |
| 29 | + /// |
| 30 | + /// It's also enforced by `rustfmt` when the `force_explicit_abi` option is enabled. See: |
| 31 | + /// [Configuring Rustfmt](https://rust-lang.github.io/rustfmt/?version=master&search=#force_explicit_abi) |
| 32 | + /// |
| 33 | + /// ### Example |
| 34 | + /// ```no_run |
| 35 | + /// extern fn foo() {} |
| 36 | + /// |
| 37 | + /// extern { |
| 38 | + /// fn bar(); |
| 39 | + /// } |
| 40 | + /// ``` |
| 41 | + /// Use instead: |
| 42 | + /// ```no_run |
| 43 | + /// extern "C" fn foo() {} |
| 44 | + /// |
| 45 | + /// extern "C" { |
| 46 | + /// fn bar(); |
| 47 | + /// } |
| 48 | + /// ``` |
| 49 | + #[clippy::version = "1.83.0"] |
| 50 | + pub EXTERN_WITHOUT_ABI, |
| 51 | + style, |
| 52 | + "`extern` missing explicit ABI" |
| 53 | +} |
| 54 | + |
| 55 | +declare_lint_pass!(ExternWithoutAbi => [EXTERN_WITHOUT_ABI]); |
| 56 | + |
| 57 | +impl<'tcx> LateLintPass<'tcx> for ExternWithoutAbi { |
| 58 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 59 | + if let ItemKind::ForeignMod { abi: Abi::C { .. }, .. } = item.kind |
| 60 | + && !in_external_macro(cx.sess(), item.span) |
| 61 | + && let snippet = snippet(cx.sess(), item.span, "").as_ref() |
| 62 | + && is_extern_followed_by(OPEN_BRACE, snippet) |
| 63 | + && !is_from_proc_macro(cx, item) |
| 64 | + { |
| 65 | + emit_lint(cx, item.span, snippet); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + fn check_fn( |
| 70 | + &mut self, |
| 71 | + cx: &LateContext<'tcx>, |
| 72 | + kind: FnKind<'tcx>, |
| 73 | + _: &'tcx FnDecl<'tcx>, |
| 74 | + body: &'tcx Body<'tcx>, |
| 75 | + span: Span, |
| 76 | + _: LocalDefId, |
| 77 | + ) { |
| 78 | + if let FnKind::ItemFn(_, _, header) = kind |
| 79 | + && let FnHeader { abi: Abi::C { .. }, .. } = header |
| 80 | + && !in_external_macro(cx.sess(), span) |
| 81 | + && let snippet = snippet(cx.sess(), span, "").as_ref() |
| 82 | + && is_extern_followed_by(FN, snippet) |
| 83 | + && let hir_id = cx.tcx.hir().body_owner(body.id()) |
| 84 | + && !is_from_proc_macro(cx, &(&kind, body, hir_id, span)) |
| 85 | + { |
| 86 | + emit_lint(cx, span, snippet); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn is_extern_followed_by(item: &str, snippet: &str) -> bool { |
| 92 | + let mut tokens = snippet.split_whitespace(); |
| 93 | + |
| 94 | + if let (Some(EXTERN), Some(i)) = (tokens.next(), tokens.next()) |
| 95 | + && i.starts_with(item) |
| 96 | + { |
| 97 | + return true; |
| 98 | + } |
| 99 | + false |
| 100 | +} |
| 101 | + |
| 102 | +fn emit_lint(cx: &LateContext<'_>, span: Span, snippet: &str) { |
| 103 | + let sugg = snippet.replacen(EXTERN, format!("{EXTERN} {ABI}").as_str(), 1); |
| 104 | + span_lint_and_sugg( |
| 105 | + cx, |
| 106 | + EXTERN_WITHOUT_ABI, |
| 107 | + span, |
| 108 | + LINT_MSG, |
| 109 | + LINT_HELP_MSG, |
| 110 | + sugg, |
| 111 | + Applicability::MachineApplicable, |
| 112 | + ); |
| 113 | +} |
0 commit comments