1 //! lint on if expressions with an else if, but without a final else branch
3 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass, in_external_macro, LintContext};
4 use rustc::{declare_tool_lint, lint_array};
7 use crate::utils::span_lint_and_sugg;
9 /// **What it does:** Checks for usage of if expressions with an `else if` branch,
10 /// but without a final `else` branch.
12 /// **Why is this bad?** Some coding guidelines require this (e.g. MISRA-C:2004 Rule 14.10).
14 /// **Known problems:** None.
18 /// if x.is_positive() {
20 /// } else if x.is_negative() {
28 /// if x.is_positive() {
30 /// } else if x.is_negative() {
33 /// // we don't care about zero
36 declare_clippy_lint! {
37 pub ELSE_IF_WITHOUT_ELSE,
39 "if expression with an `else if`, but without a final `else` branch"
42 #[derive(Copy, Clone)]
43 pub struct ElseIfWithoutElse;
45 impl LintPass for ElseIfWithoutElse {
46 fn get_lints(&self) -> LintArray {
47 lint_array!(ELSE_IF_WITHOUT_ELSE)
51 impl EarlyLintPass for ElseIfWithoutElse {
52 fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
53 if in_external_macro(cx.sess(), item.span) {
57 while let ExprKind::If(_, _, Some(ref els)) = item.node {
58 if let ExprKind::If(_, _, None) = els.node {
63 "if expression with an `else if`, but without a final `else`",
64 "add an `else` block here",