mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-26 04:17:21 +00:00
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`... And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun... Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work. ```nu mv v0.0.212 rust-1.00.0; mv beta rust-1.57.0; mv master rust-1.58.0; let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path); let versions = ( ls | where name =~ "rust-" | select name | format {name}/lints.json | each { open $it | select id | insert version $it | str substring "5,11" version} | group-by id | rotate counter-clockwise id version | update version {get version | first 1} | flatten | select id version); $paths | each { |row| let version = ($versions | where id == ($row.id) | format {version}) let idu = ($row.id | str upcase) $"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)" } | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh"; ``` And this still has some problems, but at this point I just want to be done -.-
172 lines
5.7 KiB
Rust
172 lines
5.7 KiB
Rust
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
|
use rustc_ast::ast::LitKind;
|
|
use rustc_errors::Applicability;
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
|
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, PatKind};
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
use rustc_middle::hir::map::Map;
|
|
use rustc_middle::lint::in_external_macro;
|
|
use rustc_middle::ty;
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
use rustc_span::symbol::SymbolStr;
|
|
use rustc_span::{sym, Span};
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Checks for `match` expressions modifying the case of a string with non-compliant arms
|
|
///
|
|
/// ### Why is this bad?
|
|
/// The arm is unreachable, which is likely a mistake
|
|
///
|
|
/// ### Example
|
|
/// ```rust
|
|
/// # let text = "Foo";
|
|
///
|
|
/// match &*text.to_ascii_lowercase() {
|
|
/// "foo" => {},
|
|
/// "Bar" => {},
|
|
/// _ => {},
|
|
/// }
|
|
/// ```
|
|
/// Use instead:
|
|
/// ```rust
|
|
/// # let text = "Foo";
|
|
///
|
|
/// match &*text.to_ascii_lowercase() {
|
|
/// "foo" => {},
|
|
/// "bar" => {},
|
|
/// _ => {},
|
|
/// }
|
|
/// ```
|
|
#[clippy::version = "1.58.0"]
|
|
pub MATCH_STR_CASE_MISMATCH,
|
|
correctness,
|
|
"creation of a case altering match expression with non-compliant arms"
|
|
}
|
|
|
|
declare_lint_pass!(MatchStrCaseMismatch => [MATCH_STR_CASE_MISMATCH]);
|
|
|
|
#[derive(Debug)]
|
|
enum CaseMethod {
|
|
LowerCase,
|
|
AsciiLowerCase,
|
|
UpperCase,
|
|
AsciiUppercase,
|
|
}
|
|
|
|
impl LateLintPass<'_> for MatchStrCaseMismatch {
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|
if_chain! {
|
|
if !in_external_macro(cx.tcx.sess, expr.span);
|
|
if let ExprKind::Match(match_expr, arms, MatchSource::Normal) = expr.kind;
|
|
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(match_expr).kind();
|
|
if let ty::Str = ty.kind();
|
|
then {
|
|
let mut visitor = MatchExprVisitor {
|
|
cx,
|
|
case_method: None,
|
|
};
|
|
|
|
visitor.visit_expr(match_expr);
|
|
|
|
if let Some(case_method) = visitor.case_method {
|
|
if let Some((bad_case_span, bad_case_str)) = verify_case(&case_method, arms) {
|
|
lint(cx, &case_method, bad_case_span, &bad_case_str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MatchExprVisitor<'a, 'tcx> {
|
|
cx: &'a LateContext<'tcx>,
|
|
case_method: Option<CaseMethod>,
|
|
}
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for MatchExprVisitor<'a, 'tcx> {
|
|
type Map = Map<'tcx>;
|
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
NestedVisitorMap::None
|
|
}
|
|
|
|
fn visit_expr(&mut self, ex: &'tcx Expr<'_>) {
|
|
match ex.kind {
|
|
ExprKind::MethodCall(segment, _, [receiver], _)
|
|
if self.case_altered(&*segment.ident.as_str(), receiver) => {},
|
|
_ => walk_expr(self, ex),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, 'tcx> MatchExprVisitor<'a, 'tcx> {
|
|
fn case_altered(&mut self, segment_ident: &str, receiver: &Expr<'_>) -> bool {
|
|
if let Some(case_method) = get_case_method(segment_ident) {
|
|
let ty = self.cx.typeck_results().expr_ty(receiver).peel_refs();
|
|
|
|
if is_type_diagnostic_item(self.cx, ty, sym::String) || ty.kind() == &ty::Str {
|
|
self.case_method = Some(case_method);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
false
|
|
}
|
|
}
|
|
|
|
fn get_case_method(segment_ident_str: &str) -> Option<CaseMethod> {
|
|
match segment_ident_str {
|
|
"to_lowercase" => Some(CaseMethod::LowerCase),
|
|
"to_ascii_lowercase" => Some(CaseMethod::AsciiLowerCase),
|
|
"to_uppercase" => Some(CaseMethod::UpperCase),
|
|
"to_ascii_uppercase" => Some(CaseMethod::AsciiUppercase),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn verify_case<'a>(case_method: &'a CaseMethod, arms: &'a [Arm<'_>]) -> Option<(Span, SymbolStr)> {
|
|
let case_check = match case_method {
|
|
CaseMethod::LowerCase => |input: &str| -> bool { input.chars().all(|c| c.to_lowercase().next() == Some(c)) },
|
|
CaseMethod::AsciiLowerCase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_uppercase()) },
|
|
CaseMethod::UpperCase => |input: &str| -> bool { input.chars().all(|c| c.to_uppercase().next() == Some(c)) },
|
|
CaseMethod::AsciiUppercase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_lowercase()) },
|
|
};
|
|
|
|
for arm in arms {
|
|
if_chain! {
|
|
if let PatKind::Lit(Expr {
|
|
kind: ExprKind::Lit(lit),
|
|
..
|
|
}) = arm.pat.kind;
|
|
if let LitKind::Str(symbol, _) = lit.node;
|
|
let input = symbol.as_str();
|
|
if !case_check(&input);
|
|
then {
|
|
return Some((lit.span, input));
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
fn lint(cx: &LateContext<'_>, case_method: &CaseMethod, bad_case_span: Span, bad_case_str: &str) {
|
|
let (method_str, suggestion) = match case_method {
|
|
CaseMethod::LowerCase => ("to_lowercase", bad_case_str.to_lowercase()),
|
|
CaseMethod::AsciiLowerCase => ("to_ascii_lowercase", bad_case_str.to_ascii_lowercase()),
|
|
CaseMethod::UpperCase => ("to_uppercase", bad_case_str.to_uppercase()),
|
|
CaseMethod::AsciiUppercase => ("to_ascii_uppercase", bad_case_str.to_ascii_uppercase()),
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
cx,
|
|
MATCH_STR_CASE_MISMATCH,
|
|
bad_case_span,
|
|
"this `match` arm has a differing case than its expression",
|
|
&*format!("consider changing the case of this arm to respect `{}`", method_str),
|
|
format!("\"{}\"", suggestion),
|
|
Applicability::MachineApplicable,
|
|
);
|
|
}
|