-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
352 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_from_proc_macro; | ||
use clippy_utils::source::snippet; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def_id::LocalDefId; | ||
use rustc_hir::intravisit::FnKind; | ||
use rustc_hir::{Body, FnDecl, FnHeader, Item, ItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::Span; | ||
use rustc_target::spec::abi::Abi; | ||
|
||
const LINT_MSG: &str = "`extern` missing explicit ABI"; | ||
const LINT_HELP_MSG: &str = "consider using"; | ||
|
||
const EXTERN: &str = "extern"; | ||
const FN: &str = "fn"; | ||
const OPEN_BRACE: &str = "{"; | ||
const ABI: &str = r#""C""#; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of `extern` without an explicit ABI. | ||
/// | ||
/// ### Why is this bad? | ||
/// Explicitly declaring the ABI is the recommended convention. See: | ||
/// [Rust Style Guide - `extern` items](https://doc.rust-lang.org/nightly/style-guide/items.html#extern-items) | ||
/// | ||
/// It's also enforced by `rustfmt` when the `force_explicit_abi` option is enabled. See: | ||
/// [Configuring Rustfmt](https://rust-lang.github.io/rustfmt/?version=master&search=#force_explicit_abi) | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// extern fn foo() {} | ||
/// | ||
/// extern { | ||
/// fn bar(); | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// extern "C" fn foo() {} | ||
/// | ||
/// extern "C" { | ||
/// fn bar(); | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.83.0"] | ||
pub EXTERN_WITHOUT_ABI, | ||
style, | ||
"`extern` missing explicit ABI" | ||
} | ||
|
||
declare_lint_pass!(ExternWithoutAbi => [EXTERN_WITHOUT_ABI]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ExternWithoutAbi { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { | ||
if let ItemKind::ForeignMod { abi: Abi::C { .. }, .. } = item.kind | ||
&& !in_external_macro(cx.sess(), item.span) | ||
&& let snippet = snippet(cx.sess(), item.span, "").as_ref() | ||
&& is_extern_followed_by(OPEN_BRACE, snippet) | ||
&& !is_from_proc_macro(cx, item) | ||
{ | ||
emit_lint(cx, item.span, snippet); | ||
} | ||
} | ||
|
||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'tcx>, | ||
kind: FnKind<'tcx>, | ||
_: &'tcx FnDecl<'tcx>, | ||
body: &'tcx Body<'tcx>, | ||
span: Span, | ||
_: LocalDefId, | ||
) { | ||
if let FnKind::ItemFn(_, _, header) = kind | ||
&& let FnHeader { abi: Abi::C { .. }, .. } = header | ||
&& !in_external_macro(cx.sess(), span) | ||
&& let snippet = snippet(cx.sess(), span, "").as_ref() | ||
&& is_extern_followed_by(FN, snippet) | ||
&& let hir_id = cx.tcx.hir().body_owner(body.id()) | ||
&& !is_from_proc_macro(cx, &(&kind, body, hir_id, span)) | ||
{ | ||
emit_lint(cx, span, snippet); | ||
} | ||
} | ||
} | ||
|
||
fn is_extern_followed_by(item: &str, snippet: &str) -> bool { | ||
let mut tokens = snippet.split_whitespace(); | ||
|
||
if let (Some(EXTERN), Some(i)) = (tokens.next(), tokens.next()) | ||
&& i.starts_with(item) | ||
{ | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
fn emit_lint(cx: &LateContext<'_>, span: Span, snippet: &str) { | ||
let sugg = snippet.replacen(EXTERN, format!("{EXTERN} {ABI}").as_str(), 1); | ||
span_lint_and_sugg( | ||
cx, | ||
EXTERN_WITHOUT_ABI, | ||
span, | ||
LINT_MSG, | ||
LINT_HELP_MSG, | ||
sugg, | ||
Applicability::MachineApplicable, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//@aux-build:proc_macros.rs | ||
|
||
#![warn(clippy::extern_without_abi)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::{external, with_span}; | ||
|
||
#[rustfmt::skip] | ||
extern "C" fn foo() {} | ||
//~^ ERROR: `extern` missing explicit ABI | ||
|
||
#[rustfmt::skip] | ||
extern "C" | ||
fn foo_two() {} | ||
//~^^ ERROR: `extern` missing explicit ABI | ||
|
||
extern "C" fn bar() {} | ||
|
||
#[rustfmt::skip] | ||
extern | ||
"C" | ||
fn bar_two() {} | ||
|
||
extern "system" fn baz() {} | ||
|
||
#[rustfmt::skip] | ||
extern "C" { | ||
//~^ ERROR: `extern` missing explicit ABI | ||
fn qux(); | ||
} | ||
|
||
#[rustfmt::skip] | ||
extern "C" | ||
{ | ||
//~^^ ERROR: `extern` missing explicit ABI | ||
fn qux_two(); | ||
} | ||
|
||
#[rustfmt::skip] | ||
extern "C" {fn qux_three();} | ||
//~^ ERROR: `extern` missing explicit ABI | ||
|
||
extern "C" { | ||
fn grault(); | ||
} | ||
|
||
extern "system" { | ||
fn grault_two(); | ||
} | ||
|
||
external! { | ||
extern fn waldo() {} | ||
} | ||
|
||
with_span! { | ||
span | ||
extern fn waldo_two() {} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//@aux-build:proc_macros.rs | ||
|
||
#![warn(clippy::extern_without_abi)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::{external, with_span}; | ||
|
||
#[rustfmt::skip] | ||
extern fn foo() {} | ||
//~^ ERROR: `extern` missing explicit ABI | ||
|
||
#[rustfmt::skip] | ||
extern | ||
fn foo_two() {} | ||
//~^^ ERROR: `extern` missing explicit ABI | ||
|
||
extern "C" fn bar() {} | ||
|
||
#[rustfmt::skip] | ||
extern | ||
"C" | ||
fn bar_two() {} | ||
|
||
extern "system" fn baz() {} | ||
|
||
#[rustfmt::skip] | ||
extern { | ||
//~^ ERROR: `extern` missing explicit ABI | ||
fn qux(); | ||
} | ||
|
||
#[rustfmt::skip] | ||
extern | ||
{ | ||
//~^^ ERROR: `extern` missing explicit ABI | ||
fn qux_two(); | ||
} | ||
|
||
#[rustfmt::skip] | ||
extern {fn qux_three();} | ||
//~^ ERROR: `extern` missing explicit ABI | ||
|
||
extern "C" { | ||
fn grault(); | ||
} | ||
|
||
extern "system" { | ||
fn grault_two(); | ||
} | ||
|
||
external! { | ||
extern fn waldo() {} | ||
} | ||
|
||
with_span! { | ||
span | ||
extern fn waldo_two() {} | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.