]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/case_sensitive_file_extension_comparisons.rs
Rollup merge of #82091 - henryboisdequin:use-place-ref-more, r=RalfJung
[rust.git] / src / tools / clippy / clippy_lints / src / case_sensitive_file_extension_comparisons.rs
1 use crate::utils::paths::STRING;
2 use crate::utils::{match_def_path, span_lint_and_help};
3 use if_chain::if_chain;
4 use rustc_ast::ast::LitKind;
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, 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     /// **Known problems:** None.
20     ///
21     /// **Example:**
22     ///
23     /// ```rust
24     /// fn is_rust_file(filename: &str) -> bool {
25     ///     filename.ends_with(".rs")
26     /// }
27     /// ```
28     /// Use instead:
29     /// ```rust
30     /// fn is_rust_file(filename: &str) -> bool {
31     ///     filename.rsplit('.').next().map(|ext| ext.eq_ignore_ascii_case("rs")) == Some(true)
32     /// }
33     /// ```
34     pub CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
35     pedantic,
36     "Checks for calls to ends_with with case-sensitive file extensions"
37 }
38
39 declare_lint_pass!(CaseSensitiveFileExtensionComparisons => [CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS]);
40
41 fn check_case_sensitive_file_extension_comparison(ctx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Span> {
42     if_chain! {
43         if let ExprKind::MethodCall(PathSegment { ident, .. }, _, [obj, extension, ..], span) = expr.kind;
44         if ident.as_str() == "ends_with";
45         if let ExprKind::Lit(Spanned { node: LitKind::Str(ext_literal, ..), ..}) = extension.kind;
46         if (2..=6).contains(&ext_literal.as_str().len());
47         if ext_literal.as_str().starts_with('.');
48         if ext_literal.as_str().chars().skip(1).all(|c| c.is_uppercase() || c.is_digit(10))
49             || ext_literal.as_str().chars().skip(1).all(|c| c.is_lowercase() || c.is_digit(10));
50         then {
51             let mut ty = ctx.typeck_results().expr_ty(obj);
52             ty = match ty.kind() {
53                 ty::Ref(_, ty, ..) => ty,
54                 _ => ty
55             };
56
57             match ty.kind() {
58                 ty::Str => {
59                     return Some(span);
60                 },
61                 ty::Adt(&ty::AdtDef { did, .. }, _) => {
62                     if match_def_path(ctx, did, &STRING) {
63                         return Some(span);
64                     }
65                 },
66                 _ => { return None; }
67             }
68         }
69     }
70     None
71 }
72
73 impl LateLintPass<'tcx> for CaseSensitiveFileExtensionComparisons {
74     fn check_expr(&mut self, ctx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
75         if let Some(span) = check_case_sensitive_file_extension_comparison(ctx, expr) {
76             span_lint_and_help(
77                 ctx,
78                 CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
79                 span,
80                 "case-sensitive file extension comparison",
81                 None,
82                 "consider using a case-insensitive comparison instead",
83             );
84         }
85     }
86 }