Skip to content

Commit 55bfaa1

Browse files
committed
Fixed macro edge case for semicolon_if_nothing_returned lint
1 parent f907986 commit 55bfaa1

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

clippy_lints/src/semicolon_if_nothing_returned.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use crate::utils::{match_def_path, paths, span_lint_and_then, sugg};
1+
use crate::utils::{in_macro, span_lint_and_then, sugg};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::*;
55
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::ty;
76
use rustc_session::{declare_lint_pass, declare_tool_lint};
87

98
declare_clippy_lint! {
@@ -20,13 +19,13 @@ declare_clippy_lint! {
2019
///
2120
/// ```rust
2221
/// fn main() {
23-
/// println!("Hello world")
22+
/// println!("Hello world")
2423
/// }
2524
/// ```
2625
/// Use instead:
2726
/// ```rust
2827
/// fn main() {
29-
/// println!("Hello world");
28+
/// println!("Hello world");
3029
/// }
3130
/// ```
3231
pub SEMICOLON_IF_NOTHING_RETURNED,
@@ -39,10 +38,19 @@ declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]
3938
impl LateLintPass<'_> for SemicolonIfNothingReturned {
4039
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
4140
if_chain! {
41+
if !in_macro(block.span);
4242
if let Some(expr) = block.expr;
4343
let t_expr = cx.typeck_results().expr_ty(expr);
4444
if t_expr.is_unit();
4545
then {
46+
match expr.kind {
47+
ExprKind::Loop(..) |
48+
ExprKind::Match(..) |
49+
ExprKind::Block(..) |
50+
ExprKind::If(..) if !in_macro(expr.span) => return,
51+
_ => (),
52+
}
53+
4654
let sugg = sugg::Sugg::hir(cx, &expr, "..");
4755
let suggestion = format!("{0};", sugg);
4856
span_lint_and_then(

tests/ui/semicolon_if_nothing_returned.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::semicolon_if_nothing_returned)]
2+
#![feature(label_break_value)]
23

34
fn get_unit() {}
45

@@ -11,8 +12,37 @@ fn hello() {
1112
get_unit()
1213
}
1314

15+
fn basic101(x: i32) {
16+
let y: i32;
17+
y = x + 1
18+
}
19+
1420
// this is fine
1521
fn print_sum(a: i32, b: i32) {
1622
println!("{}", a + b);
1723
assert_eq!(true, false);
1824
}
25+
26+
fn foo(x: i32) {
27+
let y: i32;
28+
if x < 1 {
29+
y = 4;
30+
} else {
31+
y = 5;
32+
}
33+
}
34+
35+
fn bar(x: i32) {
36+
let y: i32;
37+
match x {
38+
1 => y = 4,
39+
_ => y = 32,
40+
}
41+
}
42+
43+
fn foobar(x: i32) {
44+
let y: i32;
45+
'label: {
46+
y = x + 1;
47+
}
48+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: add `;` to terminate block
2-
--> $DIR/semicolon_if_nothing_returned.rs:7:5
2+
--> $DIR/semicolon_if_nothing_returned.rs:8:5
33
|
44
LL | println!("Hello")
55
| ^^^^^^^^^^^^^^^^^
@@ -8,10 +8,16 @@ LL | println!("Hello")
88
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
99

1010
error: add `;` to terminate block
11-
--> $DIR/semicolon_if_nothing_returned.rs:11:5
11+
--> $DIR/semicolon_if_nothing_returned.rs:12:5
1212
|
1313
LL | get_unit()
1414
| ^^^^^^^^^^ help: add `;`: `get_unit();`
1515

16-
error: aborting due to 2 previous errors
16+
error: add `;` to terminate block
17+
--> $DIR/semicolon_if_nothing_returned.rs:17:5
18+
|
19+
LL | y = x + 1
20+
| ^^^^^^^^^ help: add `;`: `y = x + 1;`
21+
22+
error: aborting due to 3 previous errors
1723

0 commit comments

Comments
 (0)