1 //! Lint on if expressions with an else if, but without a final else branch.
3 use rustc_ast::ast::{Expr, ExprKind};
4 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
5 use rustc_middle::lint::in_external_macro;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use crate::utils::span_lint_and_help;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for usage of if expressions with an `else if` branch,
12 /// but without a final `else` branch.
14 /// **Why is this bad?** Some coding guidelines require this (e.g., MISRA-C:2004 Rule 14.10).
16 /// **Known problems:** None.
23 /// if x.is_positive() {
25 /// } else if x.is_negative() {
36 /// if x.is_positive() {
38 /// } else if x.is_negative() {
41 /// // We don't care about zero.
44 pub ELSE_IF_WITHOUT_ELSE,
46 "`if` expression with an `else if`, but without a final `else` branch"
49 declare_lint_pass!(ElseIfWithoutElse => [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.kind {
58 if let ExprKind::If(_, _, None) = els.kind {
63 "`if` expression with an `else if`, but without a final `else`",
64 "add an `else` block here",