Skip to content

Commit d020fd7

Browse files
committed
Auto merge of #10260 - Niki4tap:external_macro_fp, r=xFrednet
`multiple_unsafe_ops_per_block`: Don't lint in external macros Fixes #10259 changelog: FP: [`multiple_unsafe_ops_per_block`]: No longer lints in external macros [#10260](#10260) <!-- changelog_none -->
2 parents 173fac0 + 926c5e4 commit d020fd7

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

clippy_lints/src/multiple_unsafe_ops_per_block.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use hir::{
1010
use rustc_ast::Mutability;
1111
use rustc_hir as hir;
1212
use rustc_lint::{LateContext, LateLintPass};
13+
use rustc_middle::lint::in_external_macro;
1314
use rustc_session::{declare_lint_pass, declare_tool_lint};
1415
use rustc_span::Span;
1516

@@ -66,7 +67,7 @@ declare_lint_pass!(MultipleUnsafeOpsPerBlock => [MULTIPLE_UNSAFE_OPS_PER_BLOCK])
6667

6768
impl<'tcx> LateLintPass<'tcx> for MultipleUnsafeOpsPerBlock {
6869
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
69-
if !matches!(block.rules, BlockCheckMode::UnsafeBlock(_)) {
70+
if !matches!(block.rules, BlockCheckMode::UnsafeBlock(_)) || in_external_macro(cx.tcx.sess, block.span) {
7071
return;
7172
}
7273
let mut unsafe_ops = vec![];

tests/ui/auxiliary/macro_rules.rs

+10
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,13 @@ macro_rules! almost_complete_range {
149149
let _ = '0'..'9';
150150
};
151151
}
152+
153+
#[macro_export]
154+
macro_rules! unsafe_macro {
155+
() => {
156+
unsafe {
157+
*core::ptr::null::<()>();
158+
*core::ptr::null::<()>();
159+
}
160+
};
161+
}

tests/ui/multiple_unsafe_ops_per_block.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// aux-build:macro_rules.rs
12
#![allow(unused)]
23
#![allow(deref_nullptr)]
34
#![allow(clippy::unnecessary_operation)]
45
#![allow(clippy::drop_copy)]
56
#![warn(clippy::multiple_unsafe_ops_per_block)]
67

8+
#[macro_use]
9+
extern crate macro_rules;
10+
711
use core::arch::asm;
812

913
fn raw_ptr() -> *const () {
@@ -107,4 +111,9 @@ unsafe fn read_char_good(ptr: *const u8) -> char {
107111
unsafe { core::char::from_u32_unchecked(int_value) }
108112
}
109113

114+
// no lint
115+
fn issue10259() {
116+
unsafe_macro!();
117+
}
118+
110119
fn main() {}

tests/ui/multiple_unsafe_ops_per_block.stderr

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this `unsafe` block contains 2 unsafe operations, expected only one
2-
--> $DIR/multiple_unsafe_ops_per_block.rs:32:5
2+
--> $DIR/multiple_unsafe_ops_per_block.rs:36:5
33
|
44
LL | / unsafe {
55
LL | | STATIC += 1;
@@ -8,19 +8,19 @@ LL | | }
88
| |_____^
99
|
1010
note: modification of a mutable static occurs here
11-
--> $DIR/multiple_unsafe_ops_per_block.rs:33:9
11+
--> $DIR/multiple_unsafe_ops_per_block.rs:37:9
1212
|
1313
LL | STATIC += 1;
1414
| ^^^^^^^^^^^
1515
note: unsafe function call occurs here
16-
--> $DIR/multiple_unsafe_ops_per_block.rs:34:9
16+
--> $DIR/multiple_unsafe_ops_per_block.rs:38:9
1717
|
1818
LL | not_very_safe();
1919
| ^^^^^^^^^^^^^^^
2020
= note: `-D clippy::multiple-unsafe-ops-per-block` implied by `-D warnings`
2121

2222
error: this `unsafe` block contains 2 unsafe operations, expected only one
23-
--> $DIR/multiple_unsafe_ops_per_block.rs:41:5
23+
--> $DIR/multiple_unsafe_ops_per_block.rs:45:5
2424
|
2525
LL | / unsafe {
2626
LL | | drop(u.u);
@@ -29,18 +29,18 @@ LL | | }
2929
| |_____^
3030
|
3131
note: union field access occurs here
32-
--> $DIR/multiple_unsafe_ops_per_block.rs:42:14
32+
--> $DIR/multiple_unsafe_ops_per_block.rs:46:14
3333
|
3434
LL | drop(u.u);
3535
| ^^^
3636
note: raw pointer dereference occurs here
37-
--> $DIR/multiple_unsafe_ops_per_block.rs:43:9
37+
--> $DIR/multiple_unsafe_ops_per_block.rs:47:9
3838
|
3939
LL | *raw_ptr();
4040
| ^^^^^^^^^^
4141

4242
error: this `unsafe` block contains 3 unsafe operations, expected only one
43-
--> $DIR/multiple_unsafe_ops_per_block.rs:48:5
43+
--> $DIR/multiple_unsafe_ops_per_block.rs:52:5
4444
|
4545
LL | / unsafe {
4646
LL | | asm!("nop");
@@ -50,23 +50,23 @@ LL | | }
5050
| |_____^
5151
|
5252
note: inline assembly used here
53-
--> $DIR/multiple_unsafe_ops_per_block.rs:49:9
53+
--> $DIR/multiple_unsafe_ops_per_block.rs:53:9
5454
|
5555
LL | asm!("nop");
5656
| ^^^^^^^^^^^
5757
note: unsafe method call occurs here
58-
--> $DIR/multiple_unsafe_ops_per_block.rs:50:9
58+
--> $DIR/multiple_unsafe_ops_per_block.rs:54:9
5959
|
6060
LL | sample.not_very_safe();
6161
| ^^^^^^^^^^^^^^^^^^^^^^
6262
note: modification of a mutable static occurs here
63-
--> $DIR/multiple_unsafe_ops_per_block.rs:51:9
63+
--> $DIR/multiple_unsafe_ops_per_block.rs:55:9
6464
|
6565
LL | STATIC = 0;
6666
| ^^^^^^^^^^
6767

6868
error: this `unsafe` block contains 6 unsafe operations, expected only one
69-
--> $DIR/multiple_unsafe_ops_per_block.rs:57:5
69+
--> $DIR/multiple_unsafe_ops_per_block.rs:61:5
7070
|
7171
LL | / unsafe {
7272
LL | | drop(u.u);
@@ -78,49 +78,49 @@ LL | | }
7878
| |_____^
7979
|
8080
note: union field access occurs here
81-
--> $DIR/multiple_unsafe_ops_per_block.rs:58:14
81+
--> $DIR/multiple_unsafe_ops_per_block.rs:62:14
8282
|
8383
LL | drop(u.u);
8484
| ^^^
8585
note: access of a mutable static occurs here
86-
--> $DIR/multiple_unsafe_ops_per_block.rs:59:14
86+
--> $DIR/multiple_unsafe_ops_per_block.rs:63:14
8787
|
8888
LL | drop(STATIC);
8989
| ^^^^^^
9090
note: unsafe method call occurs here
91-
--> $DIR/multiple_unsafe_ops_per_block.rs:60:9
91+
--> $DIR/multiple_unsafe_ops_per_block.rs:64:9
9292
|
9393
LL | sample.not_very_safe();
9494
| ^^^^^^^^^^^^^^^^^^^^^^
9595
note: unsafe function call occurs here
96-
--> $DIR/multiple_unsafe_ops_per_block.rs:61:9
96+
--> $DIR/multiple_unsafe_ops_per_block.rs:65:9
9797
|
9898
LL | not_very_safe();
9999
| ^^^^^^^^^^^^^^^
100100
note: raw pointer dereference occurs here
101-
--> $DIR/multiple_unsafe_ops_per_block.rs:62:9
101+
--> $DIR/multiple_unsafe_ops_per_block.rs:66:9
102102
|
103103
LL | *raw_ptr();
104104
| ^^^^^^^^^^
105105
note: inline assembly used here
106-
--> $DIR/multiple_unsafe_ops_per_block.rs:63:9
106+
--> $DIR/multiple_unsafe_ops_per_block.rs:67:9
107107
|
108108
LL | asm!("nop");
109109
| ^^^^^^^^^^^
110110

111111
error: this `unsafe` block contains 2 unsafe operations, expected only one
112-
--> $DIR/multiple_unsafe_ops_per_block.rs:101:5
112+
--> $DIR/multiple_unsafe_ops_per_block.rs:105:5
113113
|
114114
LL | unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
115115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116116
|
117117
note: unsafe function call occurs here
118-
--> $DIR/multiple_unsafe_ops_per_block.rs:101:14
118+
--> $DIR/multiple_unsafe_ops_per_block.rs:105:14
119119
|
120120
LL | unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
121121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122122
note: raw pointer dereference occurs here
123-
--> $DIR/multiple_unsafe_ops_per_block.rs:101:39
123+
--> $DIR/multiple_unsafe_ops_per_block.rs:105:39
124124
|
125125
LL | unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
126126
| ^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)