Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc9008b
commit 40c6f43
Showing
4 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! lint on if expressions with an else if, but without a final else branch | ||
|
||
use rustc::lint::*; | ||
use syntax::ast::*; | ||
|
||
use utils::{in_external_macro, span_lint_and_sugg}; | ||
|
||
/// **What it does:** Checks for usage of if expressions with an `else if` branch, | ||
/// but without a final `else` branch. | ||
/// | ||
/// **Why is this bad?** Some coding guidelines require this (e.g. MISRA-C:2004 Rule 14.10). | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// if x.is_positive() { | ||
/// a(); | ||
/// } else if x.is_negative() { | ||
/// b(); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Could be written: | ||
/// | ||
/// ```rust | ||
/// if x.is_positive() { | ||
/// a(); | ||
/// } else if x.is_negative() { | ||
/// b(); | ||
/// } else { | ||
/// // we don't care about zero | ||
/// } | ||
/// ``` | ||
declare_restriction_lint! { | ||
pub ELSE_IF_WITHOUT_ELSE, | ||
"if expression with an `else if`, but without a final `else` branch" | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct ElseIfWithoutElse; | ||
|
||
impl LintPass for ElseIfWithoutElse { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(ELSE_IF_WITHOUT_ELSE) | ||
} | ||
} | ||
|
||
impl EarlyLintPass for ElseIfWithoutElse { | ||
fn check_expr(&mut self, cx: &EarlyContext, mut item: &Expr) { | ||
if in_external_macro(cx, item.span) { | ||
return; | ||
} | ||
|
||
while let ExprKind::If(_, _, Some(ref els)) = item.node { | ||
if let ExprKind::If(_, _, None) = els.node { | ||
span_lint_and_sugg( | ||
cx, | ||
ELSE_IF_WITHOUT_ELSE, | ||
els.span, | ||
"if expression with an `else if`, but without a final `else`", | ||
"add an `else` block here", | ||
"".to_string() | ||
); | ||
} | ||
|
||
item = els; | ||
} | ||
} | ||
} |
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,50 @@ | ||
#![warn(clippy)] | ||
#![warn(else_if_without_else)] | ||
|
||
fn bla1() -> bool { unimplemented!() } | ||
fn bla2() -> bool { unimplemented!() } | ||
fn bla3() -> bool { unimplemented!() } | ||
|
||
fn main() { | ||
if bla1() { | ||
println!("if"); | ||
} | ||
|
||
if bla1() { | ||
println!("if"); | ||
} else { | ||
println!("else"); | ||
} | ||
|
||
if bla1() { | ||
println!("if"); | ||
} else if bla2() { | ||
println!("else if"); | ||
} else { | ||
println!("else") | ||
} | ||
|
||
if bla1() { | ||
println!("if"); | ||
} else if bla2() { | ||
println!("else if 1"); | ||
} else if bla3() { | ||
println!("else if 2"); | ||
} else { | ||
println!("else") | ||
} | ||
|
||
if bla1() { | ||
println!("if"); | ||
} else if bla2() { //~ ERROR else if without else | ||
println!("else if"); | ||
} | ||
|
||
if bla1() { | ||
println!("if"); | ||
} else if bla2() { | ||
println!("else if 1"); | ||
} else if bla3() { //~ ERROR else if without else | ||
println!("else if 2"); | ||
} | ||
} |
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,20 @@ | ||
error: if expression with an `else if`, but without a final `else` | ||
--> $DIR/else_if_without_else.rs:39:12 | ||
| | ||
39 | } else if bla2() { //~ ERROR else if without else | ||
| ____________^ | ||
40 | | println!("else if"); | ||
41 | | } | ||
| |_____^ help: add an `else` block here | ||
| | ||
= note: `-D else-if-without-else` implied by `-D warnings` | ||
|
||
error: if expression with an `else if`, but without a final `else` | ||
--> $DIR/else_if_without_else.rs:47:12 | ||
| | ||
47 | } else if bla3() { //~ ERROR else if without else | ||
| ____________^ | ||
48 | | println!("else if 2"); | ||
49 | | } | ||
| |_____^ help: add an `else` block here | ||
|