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