Skip to content

Commit

Permalink
no longer lints code inside macros
Browse files Browse the repository at this point in the history
replace is_internal_macro with from_expansion since where the safety
comment should go is ambiguous for macros
  • Loading branch information
koka831 committed Jan 7, 2023
1 parent c5ceb20 commit ac57ec5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 70 deletions.
7 changes: 3 additions & 4 deletions clippy_lints/src/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use rustc_hir as hir;
use rustc_hir::{Block, BlockCheckMode, ItemKind, Node, UnsafeSource};
use rustc_lexer::{tokenize, TokenKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{BytePos, Pos, Span, SyntaxContext};

Expand Down Expand Up @@ -97,7 +96,7 @@ declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS, UNNE
impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
if block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
&& !in_external_macro(cx.tcx.sess, block.span)
&& !block.span.from_expansion()
&& !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, block.hir_id)
&& !is_unsafe_from_proc_macro(cx, block.span)
&& !block_has_safety_comment(cx, block.span)
Expand Down Expand Up @@ -144,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
| hir::StmtKind::Semi(expr)
) = stmt.kind else { return };
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, stmt.hir_id)
&& !in_external_macro(cx.tcx.sess, stmt.span)
&& !stmt.span.from_expansion()
&& let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id)
&& let Some(help_span) = expr_has_unnecessary_safety_comment(cx, expr, pos)
{
Expand All @@ -160,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
}

fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
if in_external_macro(cx.tcx.sess, item.span) {
if item.span.from_expansion() {
return;
}

Expand Down
50 changes: 1 addition & 49 deletions tests/ui/undocumented_unsafe_blocks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,6 @@ LL | t!(unsafe {});
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:313:13
|
LL | unsafe {}
| ^^^^^^^^^
...
LL | t!();
| ---- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:321:5
|
Expand Down Expand Up @@ -163,30 +151,6 @@ LL | unsafe impl B for (u32) {}
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:374:13
|
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(());
| ---------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:399:13
|
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(());
| ---------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:407:5
|
Expand All @@ -195,18 +159,6 @@ LL | unsafe impl T for (i32) {}
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:399:13
|
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(u32);
| ----------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:413:5
|
Expand Down Expand Up @@ -318,5 +270,5 @@ LL | let bar = unsafe {};
|
= help: consider adding a safety comment on the preceding line

error: aborting due to 36 previous errors
error: aborting due to 32 previous errors

18 changes: 1 addition & 17 deletions tests/ui/unnecessary_safety_comment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ help: consider removing the safety comment
LL | // SAFETY:
| ^^^^^^^^^^

error: impl has unnecessary safety comment
--> $DIR/unnecessary_safety_comment.rs:33:13
|
LL | impl T for $t {}
| ^^^^^^^^^^^^^^^^
...
LL | with_safety_comment!(i32);
| ------------------------- in this macro invocation
|
help: consider removing the safety comment
--> $DIR/unnecessary_safety_comment.rs:32:13
|
LL | // Safety: unnecessary
| ^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expression has unnecessary safety comment
--> $DIR/unnecessary_safety_comment.rs:48:5
|
Expand Down Expand Up @@ -111,5 +95,5 @@ help: consider removing the safety comment
LL | // SAFETY: unnecessary
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors
error: aborting due to 8 previous errors

0 comments on commit ac57ec5

Please sign in to comment.