1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint on if expressions with an else if, but without a final else branch
12 use crate::rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::syntax::ast::*;
16 use crate::utils::span_help_and_lint;
18 /// **What it does:** Checks for usage of if expressions with an `else if` branch,
19 /// but without a final `else` branch.
21 /// **Why is this bad?** Some coding guidelines require this (e.g. MISRA-C:2004 Rule 14.10).
23 /// **Known problems:** None.
27 /// if x.is_positive() {
29 /// } else if x.is_negative() {
37 /// if x.is_positive() {
39 /// } else if x.is_negative() {
42 /// // we don't care about zero
45 declare_clippy_lint! {
46 pub ELSE_IF_WITHOUT_ELSE,
48 "if expression with an `else if`, but without a final `else` branch"
51 #[derive(Copy, Clone)]
52 pub struct ElseIfWithoutElse;
54 impl LintPass for ElseIfWithoutElse {
55 fn get_lints(&self) -> LintArray {
56 lint_array!(ELSE_IF_WITHOUT_ELSE)
60 impl EarlyLintPass for ElseIfWithoutElse {
61 fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
62 if in_external_macro(cx.sess(), item.span) {
66 while let ExprKind::If(_, _, Some(ref els)) = item.node {
67 if let ExprKind::If(_, _, None) = els.node {
72 "if expression with an `else if`, but without a final `else`",
73 "add an `else` block here",