From d9a44def1f9e982724da6790a3a771acc54d8d8e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 26 Jun 2024 13:24:51 +0100 Subject: [PATCH] [Ruff 0.5] Stabilise 11 `FURB` rules (#12043) --- crates/ruff_linter/src/codes.rs | 22 ++++++++-------- .../src/rules/refurb/rules/if_expr_min_max.rs | 6 +++-- .../rules/refurb/rules/redundant_log_base.rs | 2 -- .../rules/refurb/rules/regex_flag_alias.rs | 25 ++++++++----------- crates/ruff_workspace/src/configuration.rs | 2 -- 5 files changed, 25 insertions(+), 32 deletions(-) diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index b8e8fe5e1748d..c0c63d02fe7f4 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -1014,15 +1014,15 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { // refurb (Refurb, "101") => (RuleGroup::Preview, rules::refurb::rules::ReadWholeFile), (Refurb, "103") => (RuleGroup::Preview, rules::refurb::rules::WriteWholeFile), - (Refurb, "105") => (RuleGroup::Preview, rules::refurb::rules::PrintEmptyString), + (Refurb, "105") => (RuleGroup::Stable, rules::refurb::rules::PrintEmptyString), (Refurb, "110") => (RuleGroup::Preview, rules::refurb::rules::IfExpInsteadOfOrOperator), (Refurb, "113") => (RuleGroup::Preview, rules::refurb::rules::RepeatedAppend), (Refurb, "116") => (RuleGroup::Preview, rules::refurb::rules::FStringNumberFormat), (Refurb, "118") => (RuleGroup::Preview, rules::refurb::rules::ReimplementedOperator), - (Refurb, "129") => (RuleGroup::Preview, rules::refurb::rules::ReadlinesInFor), + (Refurb, "129") => (RuleGroup::Stable, rules::refurb::rules::ReadlinesInFor), (Refurb, "131") => (RuleGroup::Preview, rules::refurb::rules::DeleteFullSlice), (Refurb, "132") => (RuleGroup::Preview, rules::refurb::rules::CheckAndRemoveFromSet), - (Refurb, "136") => (RuleGroup::Preview, rules::refurb::rules::IfExprMinMax), + (Refurb, "136") => (RuleGroup::Stable, rules::refurb::rules::IfExprMinMax), (Refurb, "140") => (RuleGroup::Preview, rules::refurb::rules::ReimplementedStarmap), (Refurb, "142") => (RuleGroup::Preview, rules::refurb::rules::ForLoopSetMutations), (Refurb, "145") => (RuleGroup::Preview, rules::refurb::rules::SliceCopy), @@ -1030,18 +1030,18 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Refurb, "152") => (RuleGroup::Preview, rules::refurb::rules::MathConstant), (Refurb, "154") => (RuleGroup::Preview, rules::refurb::rules::RepeatedGlobal), (Refurb, "157") => (RuleGroup::Preview, rules::refurb::rules::VerboseDecimalConstructor), - (Refurb, "161") => (RuleGroup::Preview, rules::refurb::rules::BitCount), - (Refurb, "163") => (RuleGroup::Preview, rules::refurb::rules::RedundantLogBase), + (Refurb, "161") => (RuleGroup::Stable, rules::refurb::rules::BitCount), + (Refurb, "163") => (RuleGroup::Stable, rules::refurb::rules::RedundantLogBase), (Refurb, "164") => (RuleGroup::Preview, rules::refurb::rules::UnnecessaryFromFloat), (Refurb, "166") => (RuleGroup::Preview, rules::refurb::rules::IntOnSlicedStr), - (Refurb, "167") => (RuleGroup::Preview, rules::refurb::rules::RegexFlagAlias), - (Refurb, "168") => (RuleGroup::Preview, rules::refurb::rules::IsinstanceTypeNone), - (Refurb, "169") => (RuleGroup::Preview, rules::refurb::rules::TypeNoneComparison), + (Refurb, "167") => (RuleGroup::Stable, rules::refurb::rules::RegexFlagAlias), + (Refurb, "168") => (RuleGroup::Stable, rules::refurb::rules::IsinstanceTypeNone), + (Refurb, "169") => (RuleGroup::Stable, rules::refurb::rules::TypeNoneComparison), (Refurb, "171") => (RuleGroup::Preview, rules::refurb::rules::SingleItemMembershipTest), - (Refurb, "177") => (RuleGroup::Preview, rules::refurb::rules::ImplicitCwd), + (Refurb, "177") => (RuleGroup::Stable, rules::refurb::rules::ImplicitCwd), (Refurb, "180") => (RuleGroup::Preview, rules::refurb::rules::MetaClassABCMeta), - (Refurb, "181") => (RuleGroup::Preview, rules::refurb::rules::HashlibDigestHex), - (Refurb, "187") => (RuleGroup::Preview, rules::refurb::rules::ListReverseCopy), + (Refurb, "181") => (RuleGroup::Stable, rules::refurb::rules::HashlibDigestHex), + (Refurb, "187") => (RuleGroup::Stable, rules::refurb::rules::ListReverseCopy), (Refurb, "192") => (RuleGroup::Preview, rules::refurb::rules::SortedMinMax), // flake8-logging diff --git a/crates/ruff_linter/src/rules/refurb/rules/if_expr_min_max.rs b/crates/ruff_linter/src/rules/refurb/rules/if_expr_min_max.rs index c91030eb94060..1c25a17e871cd 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/if_expr_min_max.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/if_expr_min_max.rs @@ -156,14 +156,16 @@ enum MinMax { } impl MinMax { - fn reverse(self) -> Self { + #[must_use] + const fn reverse(self) -> Self { match self { Self::Min => Self::Max, Self::Max => Self::Min, } } - fn as_str(self) -> &'static str { + #[must_use] + const fn as_str(self) -> &'static str { match self { Self::Min => "min", Self::Max => "max", diff --git a/crates/ruff_linter/src/rules/refurb/rules/redundant_log_base.rs b/crates/ruff_linter/src/rules/refurb/rules/redundant_log_base.rs index 4fbc2774cf450..447a6ef704737 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/redundant_log_base.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/redundant_log_base.rs @@ -77,7 +77,6 @@ pub(crate) fn redundant_log_base(checker: &mut Checker, call: &ast::ExprCall) { if !checker .semantic() .resolve_qualified_name(&call.func) - .as_ref() .is_some_and(|qualified_name| matches!(qualified_name.segments(), ["math", "log"])) { return; @@ -90,7 +89,6 @@ pub(crate) fn redundant_log_base(checker: &mut Checker, call: &ast::ExprCall) { } else if checker .semantic() .resolve_qualified_name(base) - .as_ref() .is_some_and(|qualified_name| matches!(qualified_name.segments(), ["math", "e"])) { Base::E diff --git a/crates/ruff_linter/src/rules/refurb/rules/regex_flag_alias.rs b/crates/ruff_linter/src/rules/refurb/rules/regex_flag_alias.rs index 81facd1417b2e..f04768b0a3162 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/regex_flag_alias.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/regex_flag_alias.rs @@ -34,20 +34,19 @@ use crate::importer::ImportRequest; /// #[violation] pub struct RegexFlagAlias { - alias: &'static str, - full_name: &'static str, + flag: RegexFlag, } impl AlwaysFixableViolation for RegexFlagAlias { #[derive_message_formats] fn message(&self) -> String { - let RegexFlagAlias { alias, .. } = self; - format!("Use of regular expression alias `re.{alias}`") + let RegexFlagAlias { flag } = self; + format!("Use of regular expression alias `re.{}`", flag.alias()) } fn fix_title(&self) -> String { - let RegexFlagAlias { full_name, .. } = self; - format!("Replace with `re.{full_name}`") + let RegexFlagAlias { flag } = self; + format!("Replace with `re.{}`", flag.full_name()) } } @@ -75,13 +74,7 @@ pub(crate) fn regex_flag_alias(checker: &mut Checker, expr: &Expr) { return; }; - let mut diagnostic = Diagnostic::new( - RegexFlagAlias { - alias: flag.alias(), - full_name: flag.full_name(), - }, - expr.range(), - ); + let mut diagnostic = Diagnostic::new(RegexFlagAlias { flag }, expr.range()); diagnostic.try_set_fix(|| { let (edit, binding) = checker.importer().get_or_import_symbol( &ImportRequest::import("re", flag.full_name()), @@ -109,7 +102,8 @@ enum RegexFlag { } impl RegexFlag { - fn alias(self) -> &'static str { + #[must_use] + const fn alias(self) -> &'static str { match self { Self::Ascii => "A", Self::IgnoreCase => "I", @@ -122,7 +116,8 @@ impl RegexFlag { } } - fn full_name(self) -> &'static str { + #[must_use] + const fn full_name(self) -> &'static str { match self { Self::Ascii => "ASCII", Self::IgnoreCase => "IGNORECASE", diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 63a304602e322..e825a714a6d7e 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -1484,8 +1484,6 @@ mod tests { use crate::options::PydocstyleOptions; const PREVIEW_RULES: &[Rule] = &[ - Rule::IsinstanceTypeNone, - Rule::IfExprMinMax, Rule::ReimplementedStarmap, Rule::SliceCopy, Rule::TooManyPublicMethods,