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 } ;
22use if_chain:: if_chain;
33use rustc_errors:: Applicability ;
4- use rustc_hir:: * ;
4+ use rustc_hir:: { Block , ExprKind } ;
55use rustc_lint:: { LateContext , LateLintPass } ;
66use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
77
@@ -11,7 +11,6 @@ declare_clippy_lint! {
1111 ///
1212 /// **Why is this bad?** The semicolon might be optional but when
1313 /// extending the block with new code, it doesn't require a change in previous last line.
14- /// It's also more idiomatic.
1514 ///
1615 /// **Known problems:** None.
1716 ///
@@ -29,7 +28,7 @@ declare_clippy_lint! {
2928 /// }
3029 /// ```
3130 pub SEMICOLON_IF_NOTHING_RETURNED ,
32- pedantic ,
31+ restriction ,
3332 "add a semicolon if nothing is returned"
3433}
3534
@@ -42,31 +41,25 @@ impl LateLintPass<'_> for SemicolonIfNothingReturned {
4241 if let Some ( expr) = block. expr;
4342 let t_expr = cx. typeck_results( ) . expr_ty( expr) ;
4443 if t_expr. is_unit( ) ;
44+ if let snippet = snippet_with_macro_callsite( cx, expr. span, "}" ) ;
45+ if !snippet. ends_with( '}' ) ;
4546 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 ;
5250 }
5351
5452 let sugg = sugg:: Sugg :: hir( cx, & expr, ".." ) ;
5553 let suggestion = format!( "{0};" , sugg) ;
56- span_lint_and_then (
54+ span_lint_and_sugg (
5755 cx,
5856 SEMICOLON_IF_NOTHING_RETURNED ,
5957 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+ ) ;
7063 }
7164 }
7265 }
0 commit comments