Skip to content

Commit eb9c669

Browse files
committed
First version of the lint
1 parent a507c27 commit eb9c669

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,7 @@ Released 2018-09-13
21852185
[`same_item_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push
21862186
[`search_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
21872187
[`self_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_assignment
2188+
[`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
21882189
[`serde_api_misuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse
21892190
[`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse
21902191
[`shadow_same`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_same

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ mod regex;
310310
mod repeat_once;
311311
mod returns;
312312
mod self_assignment;
313+
mod semicolon_if_nothing_returned;
313314
mod serde_api;
314315
mod shadow;
315316
mod single_component_path_imports;
@@ -876,6 +877,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
876877
&returns::LET_AND_RETURN,
877878
&returns::NEEDLESS_RETURN,
878879
&self_assignment::SELF_ASSIGNMENT,
880+
&semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED,
879881
&serde_api::SERDE_API_MISUSE,
880882
&shadow::SHADOW_REUSE,
881883
&shadow::SHADOW_SAME,
@@ -1237,6 +1239,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12371239
store.register_late_pass(|| box manual_unwrap_or::ManualUnwrapOr);
12381240
store.register_late_pass(|| box manual_ok_or::ManualOkOr);
12391241
store.register_late_pass(|| box float_equality_without_abs::FloatEqualityWithoutAbs);
1242+
store.register_late_pass(|| box semicolon_if_nothing_returned::SemicolonIfNothingReturned);
12401243
store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync);
12411244
let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::<FxHashSet<_>>();
12421245
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));
@@ -1364,6 +1367,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13641367
LintId::of(&ranges::RANGE_PLUS_ONE),
13651368
LintId::of(&redundant_else::REDUNDANT_ELSE),
13661369
LintId::of(&ref_option_ref::REF_OPTION_REF),
1370+
LintId::of(&semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED),
13671371
LintId::of(&shadow::SHADOW_UNRELATED),
13681372
LintId::of(&strings::STRING_ADD_ASSIGN),
13691373
LintId::of(&trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use crate::utils::{match_def_path, paths, span_lint_and_then, sugg};
2+
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
4+
use rustc_hir::*;
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_middle::ty;
7+
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
9+
declare_clippy_lint! {
10+
/// **What it does:**
11+
///
12+
/// **Why is this bad?**
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
///
18+
/// ```rust
19+
/// // example code where clippy issues a warning
20+
/// ```
21+
/// Use instead:
22+
/// ```rust
23+
/// // example code which does not raise clippy warning
24+
/// ```
25+
pub SEMICOLON_IF_NOTHING_RETURNED,
26+
pedantic,
27+
"default lint description"
28+
}
29+
30+
declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]);
31+
32+
impl LateLintPass<'_> for SemicolonIfNothingReturned {
33+
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
34+
if_chain! {
35+
if let Some(expr) = block.expr;
36+
let t_expr = cx.typeck_results().expr_ty(expr);
37+
if t_expr.is_unit();
38+
then {
39+
let sugg = sugg::Sugg::hir(cx, &expr, "..");
40+
let suggestion = format!("{0};", sugg);
41+
span_lint_and_then(
42+
cx,
43+
SEMICOLON_IF_NOTHING_RETURNED,
44+
expr.span,
45+
"add `;` to terminate block",
46+
| diag | {
47+
diag.span_suggestion(
48+
expr.span,
49+
"add `;`",
50+
suggestion,
51+
Applicability::MaybeIncorrect,
52+
);
53+
}
54+
)
55+
}
56+
}
57+
}
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::semicolon_if_nothing_returned)]
2+
3+
fn get_unit() {}
4+
5+
// the functions below trigger the lint
6+
fn main() {
7+
println!("Hello")
8+
}
9+
10+
fn hello() {
11+
get_unit()
12+
}
13+
14+
// this is fine
15+
fn print_sum(a: i32, b: i32) {
16+
println!("{}", a + b);
17+
assert_eq!(true, false);
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: add `;` to terminate block
2+
--> $DIR/semicolon_if_nothing_returned.rs:7:5
3+
|
4+
LL | println!("Hello")
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::semicolon-if-nothing-returned` implied by `-D warnings`
8+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
9+
10+
error: add `;` to terminate block
11+
--> $DIR/semicolon_if_nothing_returned.rs:11:5
12+
|
13+
LL | get_unit()
14+
| ^^^^^^^^^^ help: add `;`: `get_unit();`
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)