forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#4 from surechen/stable
add lint extern_without_repr
- Loading branch information
Showing
7 changed files
with
166 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use rustc_hir::{ForeignItemKind, Item, ItemKind, Node}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use rustc_errors::Applicability; | ||
//use rustc_hir::{Item, ItemKind}; | ||
use clippy_utils::ty::walk_ptrs_hir_ty; | ||
use if_chain::if_chain; | ||
use rustc_hir_analysis::hir_ty_to_ty; | ||
use rustc_target::spec::abi::Abi; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// struct Foo3 { | ||
/// a: libc::c_char, | ||
/// b: libc::c_int, | ||
/// c: libc::c_longlong, | ||
/// } | ||
/// extern "C" fn c_abi_fn4(arg_one: u32, arg_two: *const Foo3) {} | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// #[repr(C)] | ||
/// struct Foo3 { | ||
/// a: libc::c_char, | ||
/// b: libc::c_int, | ||
/// c: libc::c_longlong, | ||
/// } | ||
/// extern "C" fn c_abi_fn4(arg_one: u32, arg_two: *const Foo3) {} | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub EXTERN_WITHOUT_REPR, | ||
pedantic, | ||
"Should use repr to specifing data layout when struct is used in FFI" | ||
} | ||
declare_lint_pass!(ExternWithoutRepr => [EXTERN_WITHOUT_REPR]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ExternWithoutRepr { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { | ||
let msg = "Should use repr to specifing data layout when struct is used in FFI"; | ||
if let ItemKind::Fn(fn_sig, _, _) = &item.kind { | ||
let mut app = Applicability::MaybeIncorrect; | ||
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut app); | ||
if let Some((fn_attrs, _)) = snippet.split_once("fn") { | ||
if fn_attrs.contains("extern \"C\"") { | ||
for i in 0..fn_sig.decl.inputs.len() { | ||
let t = hir_ty_to_ty(cx.tcx, walk_ptrs_hir_ty(&fn_sig.decl.inputs[i])); | ||
if let Some(adt) = t.ty_adt_def() { | ||
let repr = adt.repr(); | ||
if repr.packed() || repr.transparent() || repr.c() || repr.align.is_some() { | ||
continue; | ||
} | ||
let struct_span = cx.tcx.def_span(adt.did()); | ||
span_lint_and_then(cx, EXTERN_WITHOUT_REPR, struct_span, msg, |_| {}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if_chain! { | ||
if let ItemKind::ForeignMod { abi, items } = &item.kind; | ||
if let Abi::C { unwind: _ } = abi; | ||
then { | ||
for i in 0..items.len() { | ||
if let Some(Node::ForeignItem(f)) = cx.tcx.hir().find(items[i].id.hir_id()) { | ||
if let ForeignItemKind::Fn(decl, ..) = f.kind { | ||
for j in 0..decl.inputs.len() { | ||
let t = hir_ty_to_ty(cx.tcx, walk_ptrs_hir_ty(&decl.inputs[j])); | ||
if let Some(adt) = t.ty_adt_def() { | ||
let repr = adt.repr(); | ||
if repr.packed() | ||
|| repr.transparent() | ||
|| repr.c() | ||
|| repr.simd() | ||
|| repr.align.is_some() | ||
{ | ||
continue; | ||
} | ||
let struct_span = cx.tcx.def_span(adt.did()); | ||
span_lint_and_then(cx, EXTERN_WITHOUT_REPR, struct_span, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#![allow(unused)] | ||
#![allow(improper_ctypes_definitions)] | ||
#![allow(improper_ctypes)] | ||
#![warn(clippy::extern_without_repr)] | ||
#![feature(rustc_private)] | ||
#![feature(core_intrinsics)] | ||
extern crate libc; | ||
|
||
#[repr(C)] | ||
struct Foo1 { | ||
a: i32, | ||
b: i64, | ||
c: u128, | ||
} | ||
|
||
#[repr(packed)] | ||
#[derive(Debug)] | ||
struct Foo2 { | ||
a: libc::c_char, | ||
b: libc::c_int, | ||
c: libc::c_longlong, | ||
} | ||
|
||
struct Foo3 { | ||
a: libc::c_char, | ||
b: libc::c_int, | ||
c: libc::c_longlong, | ||
} | ||
|
||
extern "C" fn c_abi_fn1(arg_one: u32, arg_two: usize) {} | ||
extern "C" fn c_abi_fn2(arg_one: u32, arg_two: Foo1) {} | ||
extern "C" fn c_abi_fn3(arg_one: u32, arg_two: *const Foo2) {} | ||
extern "C" fn c_abi_fn4(arg_one: u32, arg_two: *const Foo3) {} | ||
|
||
extern "C" { | ||
fn c_abi_in_block1(arg_one: u32, arg_two: usize); | ||
fn c_abi_in_block2(arg_one: u32, arg_two: Foo1); | ||
fn c_abi_in_block3(arg_one: u32, arg_two: Foo2); | ||
fn c_abi_in_block4(arg_one: u32, arg_two: Foo3); | ||
} | ||
|
||
fn main() { | ||
// test code goes here | ||
} |
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,10 @@ | ||
error: Should use repr to specifing data layout when struct is used in FFI | ||
--> $DIR/extern_without_repr.rs:24:1 | ||
| | ||
LL | struct Foo3 { | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::extern-without-repr` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
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