1 //! Lint on if expressions with an else if, but without a final else branch.
3 use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
4 use rustc::{declare_lint_pass, declare_tool_lint};
7 use crate::utils::span_help_and_lint;
10 /// **What it does:** Checks for usage of if expressions with an `else if` branch,
11 /// but without a final `else` branch.
13 /// **Why is this bad?** Some coding guidelines require this (e.g., MISRA-C:2004 Rule 14.10).
15 /// **Known problems:** None.
22 /// if x.is_positive() {
24 /// } else if x.is_negative() {
35 /// if x.is_positive() {
37 /// } else if x.is_negative() {
40 /// // We don't care about zero.
43 pub ELSE_IF_WITHOUT_ELSE,
45 "if expression with an `else if`, but without a final `else` branch"
48 declare_lint_pass!(ElseIfWithoutElse => [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.sess(), item.span) {
56 while let ExprKind::If(_, _, Some(ref els)) = item.kind {
57 if let ExprKind::If(_, _, None) = els.kind {
62 "if expression with an `else if`, but without a final `else`",
63 "add an `else` block here",