Switched to snippet_with_macro_callsite

This commit is contained in:
Bastian Kersting 2021-02-06 16:56:18 +01:00
parent cd6748749a
commit 85c2b1e5f4
3 changed files with 26 additions and 26 deletions

View File

@ -1,7 +1,7 @@
use crate::utils::{in_macro, span_lint_and_then, sugg};
use crate::utils::{in_macro, span_lint_and_sugg, sugg, snippet_with_macro_callsite};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{Block, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -11,7 +11,6 @@
///
/// **Why is this bad?** The semicolon might be optional but when
/// extending the block with new code, it doesn't require a change in previous last line.
/// It's also more idiomatic.
///
/// **Known problems:** None.
///
@ -29,7 +28,7 @@
/// }
/// ```
pub SEMICOLON_IF_NOTHING_RETURNED,
pedantic,
restriction,
"add a semicolon if nothing is returned"
}
@ -42,31 +41,25 @@ fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
if let Some(expr) = block.expr;
let t_expr = cx.typeck_results().expr_ty(expr);
if t_expr.is_unit();
if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
if !snippet.ends_with('}');
then {
match expr.kind {
ExprKind::Loop(..) |
ExprKind::Match(..) |
ExprKind::Block(..) |
ExprKind::If(..) if !in_macro(expr.span) => return,
_ => (),
// filter out the desugared `for` loop
if let ExprKind::DropTemps(..) = &expr.kind {
return;
}
let sugg = sugg::Sugg::hir(cx, &expr, "..");
let suggestion = format!("{0};", sugg);
span_lint_and_then(
span_lint_and_sugg(
cx,
SEMICOLON_IF_NOTHING_RETURNED,
expr.span,
"add `;` to terminate block",
| diag | {
diag.span_suggestion(
expr.span,
"add `;`",
suggestion,
Applicability::MaybeIncorrect,
);
}
)
"consider adding a `;` to the last statement for consistent formatting",
"add a `;` here",
suggestion,
Applicability::MaybeIncorrect,
);
}
}
}

View File

@ -46,3 +46,10 @@ fn foobar(x: i32) {
y = x + 1;
}
}
fn loop_test(x: i32) {
let y: i32;
for &ext in &["stdout", "stderr", "fixed"] {
println!("{}", ext);
}
}

View File

@ -1,4 +1,4 @@
error: add `;` to terminate block
error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:8:5
|
LL | println!("Hello")
@ -7,17 +7,17 @@ LL | println!("Hello")
= note: `-D clippy::semicolon-if-nothing-returned` implied by `-D warnings`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: add `;` to terminate block
error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:12:5
|
LL | get_unit()
| ^^^^^^^^^^ help: add `;`: `get_unit();`
| ^^^^^^^^^^ help: add a `;` here: `get_unit();`
error: add `;` to terminate block
error: consider adding a `;` to the last statement for consistent formatting
--> $DIR/semicolon_if_nothing_returned.rs:17:5
|
LL | y = x + 1
| ^^^^^^^^^ help: add `;`: `y = x + 1;`
| ^^^^^^^^^ help: add a `;` here: `y = x + 1;`
error: aborting due to 3 previous errors