Auto merge of #7789 - flip1995:double_semi_if_nothing_returned, r=camsteffen

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
This commit is contained in:
bors 2021-10-08 21:30:16 +00:00
commit 22144c02c2
2 changed files with 14 additions and 2 deletions

View file

@ -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};
@ -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();

View file

@ -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!();
}