forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#6681 - 1c3t3a:1c3t3a-issue-6467, r=xFrednet,f…
…lip1995,phansch Adds a new lint that checks if there is a semicolon on the last block statement if it returns nothing changelog: Added a new lint: `SEMICOLON_IF_NOTHING_RETURNED` fixes rust-lang#6467 Adds the `SEMICOLON_IF_NOTHING_RETURNED` lint and therefore closes rust-lang#6467.
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::utils::{in_macro, snippet_with_macro_callsite, span_lint_and_sugg, sugg}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Block, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Looks for blocks of expressions and fires if the last expression returns `()` | ||
/// but is not followed by a semicolon. | ||
/// | ||
/// **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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// fn main() { | ||
/// println!("Hello world") | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// fn main() { | ||
/// println!("Hello world"); | ||
/// } | ||
/// ``` | ||
pub SEMICOLON_IF_NOTHING_RETURNED, | ||
restriction, | ||
"add a semicolon if nothing is returned" | ||
} | ||
|
||
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 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 { | ||
// filter out the desugared `for` loop | ||
if let ExprKind::DropTemps(..) = &expr.kind { | ||
return; | ||
} | ||
|
||
let sugg = sugg::Sugg::hir_with_macro_callsite(cx, &expr, ".."); | ||
let suggestion = format!("{0};", sugg); | ||
span_lint_and_sugg( | ||
cx, | ||
SEMICOLON_IF_NOTHING_RETURNED, | ||
expr.span.source_callsite(), | ||
"consider adding a `;` to the last statement for consistent formatting", | ||
"add a `;` here", | ||
suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#![warn(clippy::semicolon_if_nothing_returned)] | ||
#![feature(label_break_value)] | ||
|
||
fn get_unit() {} | ||
|
||
// the functions below trigger the lint | ||
fn main() { | ||
println!("Hello") | ||
} | ||
|
||
fn hello() { | ||
get_unit() | ||
} | ||
|
||
fn basic101(x: i32) { | ||
let y: i32; | ||
y = x + 1 | ||
} | ||
|
||
// this is fine | ||
fn print_sum(a: i32, b: i32) { | ||
println!("{}", a + b); | ||
assert_eq!(true, false); | ||
} | ||
|
||
fn foo(x: i32) { | ||
let y: i32; | ||
if x < 1 { | ||
y = 4; | ||
} else { | ||
y = 5; | ||
} | ||
} | ||
|
||
fn bar(x: i32) { | ||
let y: i32; | ||
match x { | ||
1 => y = 4, | ||
_ => y = 32, | ||
} | ||
} | ||
|
||
fn foobar(x: i32) { | ||
let y: i32; | ||
'label: { | ||
y = x + 1; | ||
} | ||
} | ||
|
||
fn loop_test(x: i32) { | ||
let y: i32; | ||
for &ext in &["stdout", "stderr", "fixed"] { | ||
println!("{}", ext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: consider adding a `;` to the last statement for consistent formatting | ||
--> $DIR/semicolon_if_nothing_returned.rs:8:5 | ||
| | ||
LL | println!("Hello") | ||
| ^^^^^^^^^^^^^^^^^ help: add a `;` here: `println!("Hello");` | ||
| | ||
= note: `-D clippy::semicolon-if-nothing-returned` implied by `-D warnings` | ||
|
||
error: consider adding a `;` to the last statement for consistent formatting | ||
--> $DIR/semicolon_if_nothing_returned.rs:12:5 | ||
| | ||
LL | get_unit() | ||
| ^^^^^^^^^^ help: add a `;` here: `get_unit();` | ||
|
||
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 a `;` here: `y = x + 1;` | ||
|
||
error: aborting due to 3 previous errors | ||
|