2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-01-30 11:06:34 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast::LitKind;
|
2022-03-04 14:28:41 -06:00
|
|
|
use rustc_data_structures::intern::Interned;
|
2021-01-30 11:06:34 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind, PathSegment};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_span::{source_map::Spanned, symbol::sym, Span};
|
2021-01-30 11:06:34 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
2021-01-30 11:06:34 -06:00
|
|
|
/// Checks for calls to `ends_with` with possible file extensions
|
|
|
|
/// and suggests to use a case-insensitive approach instead.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
2021-01-30 11:06:34 -06:00
|
|
|
/// `ends_with` is case-sensitive and may not detect files with a valid extension.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2021-01-30 11:06:34 -06:00
|
|
|
/// ```rust
|
|
|
|
/// fn is_rust_file(filename: &str) -> bool {
|
|
|
|
/// filename.ends_with(".rs")
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// fn is_rust_file(filename: &str) -> bool {
|
|
|
|
/// filename.rsplit('.').next().map(|ext| ext.eq_ignore_ascii_case("rs")) == Some(true)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-30 11:06:34 -06:00
|
|
|
pub CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
|
|
|
|
pedantic,
|
|
|
|
"Checks for calls to ends_with with case-sensitive file extensions"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(CaseSensitiveFileExtensionComparisons => [CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS]);
|
|
|
|
|
|
|
|
fn check_case_sensitive_file_extension_comparison(ctx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Span> {
|
|
|
|
if_chain! {
|
2021-12-01 11:17:50 -06:00
|
|
|
if let ExprKind::MethodCall(PathSegment { ident, .. }, [obj, extension, ..], span) = expr.kind;
|
2021-01-30 11:06:34 -06:00
|
|
|
if ident.as_str() == "ends_with";
|
|
|
|
if let ExprKind::Lit(Spanned { node: LitKind::Str(ext_literal, ..), ..}) = extension.kind;
|
|
|
|
if (2..=6).contains(&ext_literal.as_str().len());
|
|
|
|
if ext_literal.as_str().starts_with('.');
|
|
|
|
if ext_literal.as_str().chars().skip(1).all(|c| c.is_uppercase() || c.is_digit(10))
|
|
|
|
|| ext_literal.as_str().chars().skip(1).all(|c| c.is_lowercase() || c.is_digit(10));
|
|
|
|
then {
|
|
|
|
let mut ty = ctx.typeck_results().expr_ty(obj);
|
|
|
|
ty = match ty.kind() {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
ty::Ref(_, ty, ..) => *ty,
|
2021-01-30 11:06:34 -06:00
|
|
|
_ => ty
|
|
|
|
};
|
|
|
|
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Str => {
|
|
|
|
return Some(span);
|
|
|
|
},
|
2022-03-04 14:28:41 -06:00
|
|
|
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did, .. }, _)), _) => {
|
2021-10-02 18:51:01 -05:00
|
|
|
if ctx.tcx.is_diagnostic_item(sym::String, did) {
|
2021-01-30 11:06:34 -06:00
|
|
|
return Some(span);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => { return None; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for CaseSensitiveFileExtensionComparisons {
|
2021-01-30 11:06:34 -06:00
|
|
|
fn check_expr(&mut self, ctx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|
|
|
if let Some(span) = check_case_sensitive_file_extension_comparison(ctx, expr) {
|
|
|
|
span_lint_and_help(
|
|
|
|
ctx,
|
|
|
|
CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
|
|
|
|
span,
|
|
|
|
"case-sensitive file extension comparison",
|
|
|
|
None,
|
|
|
|
"consider using a case-insensitive comparison instead",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|