Skip to content

Commit

Permalink
Move compare-to-empty-string to nursery
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 21, 2023
1 parent 2b76d88 commit 1f56b57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/codes.rs
Expand Up @@ -157,7 +157,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {

// pylint
(Pylint, "C0414") => (RuleGroup::Unspecified, rules::pylint::rules::UselessImportAlias),
(Pylint, "C1901") => (RuleGroup::Unspecified, rules::pylint::rules::CompareToEmptyString),
(Pylint, "C1901") => (RuleGroup::Nursery, rules::pylint::rules::CompareToEmptyString),
(Pylint, "C3002") => (RuleGroup::Unspecified, rules::pylint::rules::UnnecessaryDirectLambdaCall),
(Pylint, "C0208") => (RuleGroup::Unspecified, rules::pylint::rules::IterationOverSet),
(Pylint, "E0100") => (RuleGroup::Unspecified, rules::pylint::rules::YieldInInit),
Expand Down
106 changes: 55 additions & 51 deletions crates/ruff/src/rules/pylint/rules/compare_to_empty_string.rs
Expand Up @@ -7,49 +7,6 @@ use ruff_macros::{derive_message_formats, violation};

use crate::checkers::ast::Checker;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub(crate) enum EmptyStringCmpOp {
Is,
IsNot,
Eq,
NotEq,
}

impl TryFrom<&CmpOp> for EmptyStringCmpOp {
type Error = anyhow::Error;

fn try_from(value: &CmpOp) -> Result<Self, Self::Error> {
match value {
CmpOp::Is => Ok(Self::Is),
CmpOp::IsNot => Ok(Self::IsNot),
CmpOp::Eq => Ok(Self::Eq),
CmpOp::NotEq => Ok(Self::NotEq),
_ => bail!("{value:?} cannot be converted to EmptyStringCmpOp"),
}
}
}

impl EmptyStringCmpOp {
pub(crate) fn into_unary(self) -> &'static str {
match self {
Self::Is | Self::Eq => "not ",
Self::IsNot | Self::NotEq => "",
}
}
}

impl std::fmt::Display for EmptyStringCmpOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr = match self {
Self::Is => "is",
Self::IsNot => "is not",
Self::Eq => "==",
Self::NotEq => "!=",
};
write!(f, "{repr}")
}
}

/// ## What it does
/// Checks for comparisons to empty strings.
///
Expand Down Expand Up @@ -83,13 +40,15 @@ pub struct CompareToEmptyString {
impl Violation for CompareToEmptyString {
#[derive_message_formats]
fn message(&self) -> String {
format!(
"`{}` can be simplified to `{}` as an empty string is falsey",
self.existing, self.replacement,
)
let CompareToEmptyString {
existing,
replacement,
} = self;
format!("`{existing}` can be simplified to `{replacement}` as an empty string is falsey",)
}
}

/// PLC1901
pub(crate) fn compare_to_empty_string(
checker: &mut Checker,
left: &Expr,
Expand All @@ -98,10 +57,12 @@ pub(crate) fn compare_to_empty_string(
) {
// Omit string comparison rules within subscripts. This is mostly commonly used within
// DataFrame and np.ndarray indexing.
for parent in checker.semantic().expr_ancestors() {
if matches!(parent, Expr::Subscript(_)) {
return;
}
if checker
.semantic()
.expr_ancestors()
.any(|parent| parent.is_subscript_expr())
{
return;
}

let mut first = true;
Expand Down Expand Up @@ -153,3 +114,46 @@ pub(crate) fn compare_to_empty_string(
}
}
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum EmptyStringCmpOp {
Is,
IsNot,
Eq,
NotEq,
}

impl TryFrom<&CmpOp> for EmptyStringCmpOp {
type Error = anyhow::Error;

fn try_from(value: &CmpOp) -> Result<Self, Self::Error> {
match value {
CmpOp::Is => Ok(Self::Is),
CmpOp::IsNot => Ok(Self::IsNot),
CmpOp::Eq => Ok(Self::Eq),
CmpOp::NotEq => Ok(Self::NotEq),
_ => bail!("{value:?} cannot be converted to EmptyStringCmpOp"),
}
}
}

impl EmptyStringCmpOp {
fn into_unary(self) -> &'static str {
match self {
Self::Is | Self::Eq => "not ",
Self::IsNot | Self::NotEq => "",
}
}
}

impl std::fmt::Display for EmptyStringCmpOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr = match self {
Self::Is => "is",
Self::IsNot => "is not",
Self::Eq => "==",
Self::NotEq => "!=",
};
write!(f, "{repr}")
}
}
3 changes: 0 additions & 3 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f56b57

Please sign in to comment.