]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/case_sensitive_file_extension_comparisons.rs
rebase and use ty::Const in patterns again
[rust.git] / clippy_lints / src / case_sensitive_file_extension_comparisons.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use if_chain::if_chain;
3 use rustc_ast::ast::LitKind;
4 use rustc_data_structures::intern::Interned;
5 use rustc_hir::{Expr, ExprKind, PathSegment};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_middle::ty;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use rustc_span::{source_map::Spanned, symbol::sym, Span};
10
11 declare_clippy_lint! {
12     /// ### What it does
13     /// Checks for calls to `ends_with` with possible file extensions
14     /// and suggests to use a case-insensitive approach instead.
15     ///
16     /// ### Why is this bad?
17     /// `ends_with` is case-sensitive and may not detect files with a valid extension.
18     ///
19     /// ### Example
20     /// ```rust
21     /// fn is_rust_file(filename: &str) -> bool {
22     ///     filename.ends_with(".rs")
23     /// }
24     /// ```
25     /// Use instead:
26     /// ```rust
27     /// fn is_rust_file(filename: &str) -> bool {
28     ///     filename.rsplit('.').next().map(|ext| ext.eq_ignore_ascii_case("rs")) == Some(true)
29     /// }
30     /// ```
31     #[clippy::version = "1.51.0"]
32     pub CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
33     pedantic,
34     "Checks for calls to ends_with with case-sensitive file extensions"
35 }
36
37 declare_lint_pass!(CaseSensitiveFileExtensionComparisons => [CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS]);
38
39 fn check_case_sensitive_file_extension_comparison(ctx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Span> {
40     if_chain! {
41         if let ExprKind::MethodCall(PathSegment { ident, .. }, [obj, extension, ..], span) = expr.kind;
42         if ident.as_str() == "ends_with";
43         if let ExprKind::Lit(Spanned { node: LitKind::Str(ext_literal, ..), ..}) = extension.kind;
44         if (2..=6).contains(&ext_literal.as_str().len());
45         if ext_literal.as_str().starts_with('.');
46         if ext_literal.as_str().chars().skip(1).all(|c| c.is_uppercase() || c.is_digit(10))
47             || ext_literal.as_str().chars().skip(1).all(|c| c.is_lowercase() || c.is_digit(10));
48         then {
49             let mut ty = ctx.typeck_results().expr_ty(obj);
50             ty = match ty.kind() {
51                 ty::Ref(_, ty, ..) => *ty,
52                 _ => ty
53             };
54
55             match ty.kind() {
56                 ty::Str => {
57                     return Some(span);
58                 },
59                 ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did, .. }, _)), _) => {
60                     if ctx.tcx.is_diagnostic_item(sym::String, did) {
61                         return Some(span);
62                     }
63                 },
64                 _ => { return None; }
65             }
66         }
67     }
68     None
69 }
70
71 impl<'tcx> LateLintPass<'tcx> for CaseSensitiveFileExtensionComparisons {
72     fn check_expr(&mut self, ctx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
73         if let Some(span) = check_case_sensitive_file_extension_comparison(ctx, expr) {
74             span_lint_and_help(
75                 ctx,
76                 CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
77                 span,
78                 "case-sensitive file extension comparison",
79                 None,
80                 "consider using a case-insensitive comparison instead",
81             );
82         }
83     }
84 }