Skip to content

Commit 24e2ec9

Browse files
committed
missing_transmute_annotations: add option to ignore in all expansions
1 parent a46af27 commit 24e2ec9

10 files changed

Lines changed: 84 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7850,6 +7850,7 @@ Released 2018-09-13
78507850
[`min-ident-chars-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#min-ident-chars-threshold
78517851
[`missing-docs-allow-unused`]: https://doc.rust-lang.org/clippy/lint_configuration.html#missing-docs-allow-unused
78527852
[`missing-docs-in-crate-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#missing-docs-in-crate-items
7853+
[`missing-transmute-annotations-in-expansions`]: https://doc.rust-lang.org/clippy/lint_configuration.html#missing-transmute-annotations-in-expansions
78537854
[`module-item-order-groupings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#module-item-order-groupings
78547855
[`module-items-ordered-within-groupings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#module-items-ordered-within-groupings
78557856
[`msrv`]: https://doc.rust-lang.org/clippy/lint_configuration.html#msrv

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,16 @@ crate. For example, `pub(crate)` items.
904904
* [`missing_docs_in_private_items`](https://rust-lang.github.io/rust-clippy/main/index.html#missing_docs_in_private_items)
905905

906906

907+
## `missing-transmute-annotations-in-expansions`
908+
Whether to check for missing transmute annotations inside expansions. For example, macros.
909+
910+
**Default Value:** `true`
911+
912+
---
913+
**Affected lints:**
914+
* [`missing_transmute_annotations`](https://rust-lang.github.io/rust-clippy/main/index.html#missing_transmute_annotations)
915+
916+
907917
## `module-item-order-groupings`
908918
The named groupings of different source item kinds within modules.
909919

clippy_config/src/conf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,9 @@ define_Conf! {
628628
/// crate. For example, `pub(crate)` items.
629629
#[lints(missing_docs_in_private_items)]
630630
missing_docs_in_crate_items("missing-docs-in-crate-items"): bool = false,
631+
/// Whether to check for missing transmute annotations inside expansions. For example, macros.
632+
#[lints(missing_transmute_annotations)]
633+
missing_transmute_annotations_in_expansions("missing-transmute-annotations-in-expansions"): bool = true,
631634
/// The named groupings of different source item kinds within modules.
632635
#[lints(arbitrary_source_item_ordering)]
633636
module_item_order_groupings("module-item-order-groupings"): SourceItemOrderingModuleItemGroupings,

clippy_lints/src/transmute/missing_transmute_annotations.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ pub(super) fn check<'tcx>(
4646
from_ty: Ty<'tcx>,
4747
to_ty: Ty<'tcx>,
4848
expr_hir_id: HirId,
49+
annotations_in_expansions: bool,
4950
) -> bool {
5051
let last = path.segments.last().unwrap();
51-
if last.ident.span.in_external_macro(cx.tcx.sess.source_map()) {
52-
// If it comes from a non-local macro, we ignore it.
52+
if last.ident.span.from_expansion()
53+
&& (!annotations_in_expansions || last.ident.span.in_external_macro(cx.tcx.sess.source_map()))
54+
{
55+
// If it comes from an expansion and this lint is disabled there or it is a non-local macro, we
56+
// ignore it.
5357
return false;
5458
}
59+
5560
let args = last.args;
5661
let missing_generic = match args {
5762
Some(args) if !args.args.is_empty() => args.args.iter().any(|arg| matches!(arg, GenericArg::Infer(_))),

clippy_lints/src/transmute/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,14 @@ impl_lint_pass!(Transmute => [
490490

491491
pub struct Transmute {
492492
msrv: Msrv,
493+
annotations_in_expansions: bool,
493494
}
494495
impl Transmute {
495496
pub fn new(conf: &'static Conf) -> Self {
496-
Self { msrv: conf.msrv.into() }
497+
Self {
498+
msrv: conf.msrv.into(),
499+
annotations_in_expansions: conf.missing_transmute_annotations_in_expansions,
500+
}
497501
}
498502

499503
/// When transmuting, a struct containing a single field works like the field.
@@ -555,7 +559,15 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
555559
| transmuting_null::check(cx, e, arg, to_ty)
556560
| transmute_null_to_fn::check(cx, e, arg, to_ty)
557561
| transmute_ptr_to_ref::check(cx, e, from_field_ty, to_ty, from_field_expr.clone(), path, self.msrv)
558-
| missing_transmute_annotations::check(cx, path, arg, from_ty, to_ty, e.hir_id)
562+
| missing_transmute_annotations::check(
563+
cx,
564+
path,
565+
arg,
566+
from_ty,
567+
to_ty,
568+
e.hir_id,
569+
self.annotations_in_expansions,
570+
)
559571
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
560572
| transmute_ptr_to_ptr::check(cx, e, from_field_ty, to_ty, from_field_expr, self.msrv)
561573
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
missing-transmute-annotations-in-expansions = false
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::missing_transmute_annotations)]
2+
3+
macro_rules! local_bad_transmute {
4+
($e:expr) => {
5+
std::mem::transmute($e)
6+
// when `in_expansions` is `true` (its default), there is a
7+
// transmute_missing_annotations error expected here
8+
};
9+
}
10+
11+
fn main() {
12+
unsafe {
13+
let mut i: i32 = 0;
14+
i = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
15+
//~^ ERROR: transmute used without annotations
16+
i = local_bad_transmute!([1u16, 2u16]);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::missing_transmute_annotations)]
2+
3+
macro_rules! local_bad_transmute {
4+
($e:expr) => {
5+
std::mem::transmute($e)
6+
// when `in_expansions` is `true` (its default), there is a
7+
// transmute_missing_annotations error expected here
8+
};
9+
}
10+
11+
fn main() {
12+
unsafe {
13+
let mut i: i32 = 0;
14+
i = std::mem::transmute([1u16, 2u16]);
15+
//~^ ERROR: transmute used without annotations
16+
i = local_bad_transmute!([1u16, 2u16]);
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: transmute used without annotations
2+
--> tests/ui-toml/missing_transmute_annotations_in_expansions/disabled.rs:14:23
3+
|
4+
LL | i = std::mem::transmute([1u16, 2u16]);
5+
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
6+
|
7+
= note: `-D clippy::missing-transmute-annotations` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`
9+
10+
error: aborting due to 1 previous error
11+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ LL | foobar = 42
7474
min-ident-chars-threshold
7575
missing-docs-allow-unused
7676
missing-docs-in-crate-items
77+
missing-transmute-annotations-in-expansions
7778
module-item-order-groupings
7879
module-items-ordered-within-groupings
7980
msrv

0 commit comments

Comments
 (0)