Skip to content

Commit

Permalink
Auto merge of #7789 - flip1995:double_semi_if_nothing_returned, r=cam…
Browse files Browse the repository at this point in the history
…steffen

Don't trigger semicolon_if_nothing_returned in expanded code

Fixes #7768

Before, this lint didn't trigger on macros. With rust-lang/rust#88175
this isn't enough anymore. In this PR a `WhileLoop` desugaring kind was
introduced. This overrides the span of expanded expressions when
lowering the while loop. So if a while loop is in a macro, the
expressions that it expands to are no longer marked with
`ExpnKind::Macro`, but with `ExpnKind::Desugaring`. In general, this is
the correct behavior and the same that is done for `ForLoop`s. It just
tripped up this lint.

r? `@camsteffen`

changelog: [`semicolon_if_nothing_returned`]: Fix regression on macros containing while loops
  • Loading branch information
bors committed Oct 8, 2021
2 parents 78e7312 + b423b85 commit 22144c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/semicolon_if_nothing_returned.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::rustc_lint::LintContext;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_macro_callsite;
use clippy_utils::{in_macro, sugg};
use clippy_utils::sugg;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Block, ExprKind};
Expand Down Expand Up @@ -39,7 +39,7 @@ declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]
impl LateLintPass<'_> for SemicolonIfNothingReturned {
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
if_chain! {
if !in_macro(block.span);
if !block.span.from_expansion();
if let Some(expr) = block.expr;
let t_expr = cx.typeck_results().expr_ty(expr);
if t_expr.is_unit();
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/semicolon_if_nothing_returned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ fn unsafe_checks() {
let mut s = MaybeUninit::<String>::uninit();
let _d = || unsafe { ptr::drop_in_place(s.as_mut_ptr()) };
}

// Issue #7768
#[rustfmt::skip]
fn macro_with_semicolon() {
macro_rules! repro {
() => {
while false {
}
};
}
repro!();
}

0 comments on commit 22144c0

Please sign in to comment.