-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
13 changed files
with
157 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_ast::visit::FnKind; | ||
use rustc_ast::{Extern, FnHeader, FnSig, ForeignMod, Item, ItemKind, NodeId}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::Span; | ||
|
||
const LINT_MSG: &str = "`extern` missing explicit ABI"; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of `extern` without an explicit ABI. | ||
/// | ||
/// ### Why is this bad? | ||
/// Explicitly declaring the ABI improves readability. | ||
// | ||
/// ### 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 EarlyLintPass for ExternWithoutAbi { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { | ||
if let ItemKind::ForeignMod(ref foreign_mod) = item.kind | ||
&& let ForeignMod { abi: None, .. } = foreign_mod | ||
{ | ||
span_lint(cx, EXTERN_WITHOUT_ABI, item.span, LINT_MSG); | ||
} | ||
} | ||
|
||
fn check_fn(&mut self, cx: &EarlyContext<'_>, kind: FnKind<'_>, _: Span, _: NodeId) { | ||
if let FnKind::Fn(_, _, sig, ..) = kind | ||
&& let FnSig { header, .. } = sig | ||
&& let FnHeader { | ||
ext: Extern::Implicit(span), | ||
.. | ||
} = header | ||
{ | ||
span_lint(cx, EXTERN_WITHOUT_ABI, *span, LINT_MSG); | ||
} | ||
} | ||
} |
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,31 @@ | ||
#![warn(clippy::extern_without_abi)] | ||
|
||
fn main() {} | ||
|
||
#[rustfmt::skip] | ||
extern fn foo() {} | ||
//~^ ERROR: `extern` missing explicit ABI | ||
|
||
extern "C" fn bar() {} | ||
|
||
extern "system" fn baz() {} | ||
|
||
#[rustfmt::skip] | ||
extern { | ||
//~^ ERROR: `extern` missing explicit ABI | ||
static QUX: std::ffi::c_int; | ||
|
||
fn quux(); | ||
} | ||
|
||
extern "C" { | ||
static CORGE: std::ffi::c_int; | ||
|
||
fn grault(); | ||
} | ||
|
||
extern "system" { | ||
static GARPLY: std::ffi::c_int; | ||
|
||
fn waldo(); | ||
} |
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,21 @@ | ||
error: `extern` missing explicit ABI | ||
--> tests/ui/extern_without_abi.rs:6:1 | ||
| | ||
LL | extern fn foo() {} | ||
| ^^^^^^ | ||
| | ||
= note: `-D clippy::extern-without-abi` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::extern_without_abi)]` | ||
|
||
error: `extern` missing explicit ABI | ||
--> tests/ui/extern_without_abi.rs:14:1 | ||
| | ||
LL | / extern { | ||
LL | | | ||
LL | | static QUX: std::ffi::c_int; | ||
... | | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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
Oops, something went wrong.