1 //! lint on if expressions with an else if, but without a final else branch
6 use utils::{in_external_macro, span_lint_and_sugg};
8 /// **What it does:** Checks for usage of if expressions with an `else if` branch,
9 /// but without a final `else` branch.
11 /// **Why is this bad?** Some coding guidelines require this (e.g. MISRA-C:2004 Rule 14.10).
13 /// **Known problems:** None.
17 /// if x.is_positive() {
19 /// } else if x.is_negative() {
27 /// if x.is_positive() {
29 /// } else if x.is_negative() {
32 /// // we don't care about zero
35 declare_clippy_lint! {
36 pub ELSE_IF_WITHOUT_ELSE,
38 "if expression with an `else if`, but without a final `else` branch"
41 #[derive(Copy, Clone)]
42 pub struct ElseIfWithoutElse;
44 impl LintPass for ElseIfWithoutElse {
45 fn get_lints(&self) -> LintArray {
46 lint_array!(ELSE_IF_WITHOUT_ELSE)
50 impl EarlyLintPass for ElseIfWithoutElse {
51 fn check_expr(&mut self, cx: &EarlyContext, mut item: &Expr) {
52 if in_external_macro(cx, item.span) {
56 while let ExprKind::If(_, _, Some(ref els)) = item.node {
57 if let ExprKind::If(_, _, None) = els.node {
62 "if expression with an `else if`, but without a final `else`",
63 "add an `else` block here",