1
- use crate :: utils:: { in_macro, span_lint_and_then , sugg} ;
1
+ use crate :: utils:: { in_macro, span_lint_and_sugg , sugg, snippet_with_macro_callsite } ;
2
2
use if_chain:: if_chain;
3
3
use rustc_errors:: Applicability ;
4
- use rustc_hir:: * ;
4
+ use rustc_hir:: { Block , ExprKind } ;
5
5
use rustc_lint:: { LateContext , LateLintPass } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
7
@@ -11,7 +11,6 @@ declare_clippy_lint! {
11
11
///
12
12
/// **Why is this bad?** The semicolon might be optional but when
13
13
/// extending the block with new code, it doesn't require a change in previous last line.
14
- /// It's also more idiomatic.
15
14
///
16
15
/// **Known problems:** None.
17
16
///
@@ -29,7 +28,7 @@ declare_clippy_lint! {
29
28
/// }
30
29
/// ```
31
30
pub SEMICOLON_IF_NOTHING_RETURNED ,
32
- pedantic ,
31
+ restriction ,
33
32
"add a semicolon if nothing is returned"
34
33
}
35
34
@@ -42,31 +41,25 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
42
41
if let Some ( expr) = block. expr;
43
42
let t_expr = cx. typeck_results( ) . expr_ty( expr) ;
44
43
if t_expr. is_unit( ) ;
44
+ if let snippet = snippet_with_macro_callsite( cx, expr. span, "}" ) ;
45
+ if !snippet. ends_with( '}' ) ;
45
46
then {
46
- match expr. kind {
47
- ExprKind :: Loop ( ..) |
48
- ExprKind :: Match ( ..) |
49
- ExprKind :: Block ( ..) |
50
- ExprKind :: If ( ..) if !in_macro( expr. span) => return ,
51
- _ => ( ) ,
47
+ // filter out the desugared `for` loop
48
+ if let ExprKind :: DropTemps ( ..) = & expr. kind {
49
+ return ;
52
50
}
53
51
54
52
let sugg = sugg:: Sugg :: hir( cx, & expr, ".." ) ;
55
53
let suggestion = format!( "{0};" , sugg) ;
56
- span_lint_and_then (
54
+ span_lint_and_sugg (
57
55
cx,
58
56
SEMICOLON_IF_NOTHING_RETURNED ,
59
57
expr. span,
60
- "add `;` to terminate block" ,
61
- | diag | {
62
- diag. span_suggestion(
63
- expr. span,
64
- "add `;`" ,
65
- suggestion,
66
- Applicability :: MaybeIncorrect ,
67
- ) ;
68
- }
69
- )
58
+ "consider adding a `;` to the last statement for consistent formatting" ,
59
+ "add a `;` here" ,
60
+ suggestion,
61
+ Applicability :: MaybeIncorrect ,
62
+ ) ;
70
63
}
71
64
}
72
65
}
0 commit comments