Skip to content

Commit

Permalink
Change rule name
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 25, 2024
1 parent 5357e17 commit 9517e9a
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 159 deletions.
9 changes: 2 additions & 7 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import decimal
from decimal import Decimal
from decimal import Decimal as dc

# Positive cases

# Errors
Decimal("0")
Decimal("-42")
Decimal(float("Infinity"))
Expand All @@ -12,11 +10,8 @@
Decimal(float("-inf"))
Decimal(float("nan"))
decimal.Decimal("0")
dc("0")

# Negative cases

# OK
Decimal(0)
Decimal("Infinity")
decimal.Decimal(0)
dc(0)
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,8 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::RedundantLogBase) {
refurb::rules::redundant_log_base(checker, call);
}
if checker.enabled(Rule::SimplifyDecimalCtor) {
refurb::rules::simplify_decimal_ctor(checker, call);
if checker.enabled(Rule::VerboseDecimalConstructor) {
refurb::rules::verbose_decimal_constructor(checker, call);
}
if checker.enabled(Rule::QuadraticListSummation) {
ruff::rules::quadratic_list_summation(checker, call);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Refurb, "145") => (RuleGroup::Preview, rules::refurb::rules::SliceCopy),
(Refurb, "148") => (RuleGroup::Preview, rules::refurb::rules::UnnecessaryEnumerate),
(Refurb, "152") => (RuleGroup::Preview, rules::refurb::rules::MathConstant),
(Refurb, "157") => (RuleGroup::Preview, rules::refurb::rules::SimplifyDecimalCtor),
(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, "167") => (RuleGroup::Preview, rules::refurb::rules::RegexFlagAlias),
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/refurb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod tests {
#[test_case(Rule::SliceCopy, Path::new("FURB145.py"))]
#[test_case(Rule::UnnecessaryEnumerate, Path::new("FURB148.py"))]
#[test_case(Rule::MathConstant, Path::new("FURB152.py"))]
#[test_case(Rule::SimplifyDecimalCtor, Path::new("FURB157.py"))]
#[test_case(Rule::VerboseDecimalConstructor, Path::new("FURB157.py"))]
#[test_case(Rule::PrintEmptyString, Path::new("FURB105.py"))]
#[test_case(Rule::ImplicitCwd, Path::new("FURB177.py"))]
#[test_case(Rule::SingleItemMembershipTest, Path::new("FURB171.py"))]
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/refurb/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub(crate) use regex_flag_alias::*;
pub(crate) use reimplemented_operator::*;
pub(crate) use reimplemented_starmap::*;
pub(crate) use repeated_append::*;
pub(crate) use simplify_decimal_ctor::*;
pub(crate) use single_item_membership_test::*;
pub(crate) use slice_copy::*;
pub(crate) use type_none_comparison::*;
pub(crate) use unnecessary_enumerate::*;
pub(crate) use verbose_decimal_constructor::*;

mod bit_count;
mod check_and_remove_from_set;
Expand All @@ -40,8 +40,8 @@ mod regex_flag_alias;
mod reimplemented_operator;
mod reimplemented_starmap;
mod repeated_append;
mod simplify_decimal_ctor;
mod single_item_membership_test;
mod slice_copy;
mod type_none_comparison;
mod unnecessary_enumerate;
mod verbose_decimal_constructor;
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ use crate::checkers::ast::Checker;
/// ## References
/// - [Python documentation: `decimal`](https://docs.python.org/3/library/decimal.html)
#[violation]
pub struct SimplifyDecimalCtor {
pub struct VerboseDecimalConstructor {
replace_old: String,
replace_new: String,
}

impl Violation for SimplifyDecimalCtor {
impl Violation for VerboseDecimalConstructor {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Always;

#[derive_message_formats]
Expand All @@ -55,7 +55,7 @@ impl Violation for SimplifyDecimalCtor {
}

/// FURB157
pub(crate) fn simplify_decimal_ctor(checker: &mut Checker, call: &ExprCall) {
pub(crate) fn verbose_decimal_constructor(checker: &mut Checker, call: &ExprCall) {
if !checker
.semantic()
.resolve_qualified_name(&call.func)
Expand Down Expand Up @@ -84,15 +84,15 @@ pub(crate) fn simplify_decimal_ctor(checker: &mut Checker, call: &ExprCall) {
.to_str()
.trim_whitespace()
.trim_start_matches('+');
let integer_string = Regex::new(r"^([\+\-]?)0*(\d+)$").unwrap();
let integer_string = Regex::new(r"^([+\-]?)0*(\d+)$").unwrap();
if !integer_string.is_match(trimmed) {
return;
};

let intg = integer_string.replace(trimmed, "$1$2").into_owned();

let mut diagnostic = Diagnostic::new(
SimplifyDecimalCtor {
VerboseDecimalConstructor {
replace_old: format!("{}(\"{}\")", decimal_constructor, str_literal.to_str()),
replace_new: format!("{decimal_constructor}({intg})"),
},
Expand Down Expand Up @@ -136,7 +136,7 @@ pub(crate) fn simplify_decimal_ctor(checker: &mut Checker, call: &ExprCall) {
}

let mut diagnostic = Diagnostic::new(
SimplifyDecimalCtor {
VerboseDecimalConstructor {
replace_old: format!("float(\"{value_float_str}\")"),
replace_new: format!("\"{value_float_str}\""),
},
Expand Down
Loading

0 comments on commit 9517e9a

Please sign in to comment.