From 5e6563821011949da38c11e32f44cd921bccf1c6 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 30 Oct 2023 10:35:48 +0530 Subject: [PATCH] Code review changes --- .../src/checkers/ast/analyze/definitions.rs | 2 +- .../src/checkers/ast/analyze/expression.rs | 8 +++---- crates/ruff_linter/src/doc_lines.rs | 12 +++++++---- .../ruff_linter/src/docstrings/extraction.rs | 13 ++++++------ crates/ruff_linter/src/docstrings/mod.rs | 5 +++-- .../rules/hardcoded_bind_all_interfaces.rs | 9 ++++---- .../rules/hardcoded_tmp_directory.rs | 9 ++++---- .../flake8_pyi/rules/docstring_in_stubs.rs | 6 +++--- .../src/rules/pydocstyle/helpers.rs | 11 ---------- .../src/rules/pydocstyle/rules/capitalized.rs | 2 +- .../rules/newline_after_last_paragraph.rs | 4 ++-- .../pylint/rules/magic_value_comparison.rs | 4 +++- .../ruff_linter/src/rules/pylint/settings.rs | 20 ++++++++---------- .../pyupgrade/rules/unicode_kind_prefix.rs | 10 ++++----- .../src/rules/refurb/rules/implicit_cwd.rs | 4 ++-- .../rules/ruff/rules/invalid_index_type.rs | 4 ++-- crates/ruff_python_codegen/src/generator.rs | 6 +++--- .../src/expression/expr_boolean_literal.rs | 9 -------- .../src/expression/expr_bytes_literal.rs | 21 ++++--------------- .../src/expression/expr_ellipsis_literal.rs | 9 -------- .../src/expression/expr_none_literal.rs | 9 -------- .../src/expression/expr_number_literal.rs | 9 -------- .../src/statement/suite.rs | 2 +- 23 files changed, 65 insertions(+), 123 deletions(-) diff --git a/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs b/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs index f4b7d3fb34be1..f4c40279096b2 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/definitions.rs @@ -170,7 +170,7 @@ pub(crate) fn definitions(checker: &mut Checker) { expr.start(), )); - if pydocstyle::helpers::should_ignore_docstring(expr) { + if expr.implicit_concatenated { #[allow(deprecated)] let location = checker.locator.compute_source_location(expr.start()); warn_user!( diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 42706e3c34aa3..b91aceb3c0e01 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -1238,19 +1238,19 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { flake8_pyi::rules::string_or_bytes_too_long(checker, expr); } } - Expr::StringLiteral(ast::ExprStringLiteral { value, unicode, .. }) => { + Expr::StringLiteral(string) => { if checker.enabled(Rule::HardcodedBindAllInterfaces) { if let Some(diagnostic) = - flake8_bandit::rules::hardcoded_bind_all_interfaces(value, expr.range()) + flake8_bandit::rules::hardcoded_bind_all_interfaces(string) { checker.diagnostics.push(diagnostic); } } if checker.enabled(Rule::HardcodedTempFile) { - flake8_bandit::rules::hardcoded_tmp_directory(checker, expr, value); + flake8_bandit::rules::hardcoded_tmp_directory(checker, string); } if checker.enabled(Rule::UnicodeKindPrefix) { - pyupgrade::rules::unicode_kind_prefix(checker, expr, *unicode); + pyupgrade::rules::unicode_kind_prefix(checker, string); } if checker.source_type.is_stub() { if checker.enabled(Rule::StringOrBytesTooLong) { diff --git a/crates/ruff_linter/src/doc_lines.rs b/crates/ruff_linter/src/doc_lines.rs index e7864be3c1786..7b69fb866920d 100644 --- a/crates/ruff_linter/src/doc_lines.rs +++ b/crates/ruff_linter/src/doc_lines.rs @@ -69,11 +69,15 @@ struct StringLinesVisitor<'a> { impl StatementVisitor<'_> for StringLinesVisitor<'_> { fn visit_stmt(&mut self, stmt: &Stmt) { - if let Stmt::Expr(ast::StmtExpr { value, range: _ }) = stmt { - if value.is_string_literal_expr() { + if let Stmt::Expr(ast::StmtExpr { + value: expr, + range: _, + }) = stmt + { + if expr.is_string_literal_expr() { for line in UniversalNewlineIterator::with_offset( - self.locator.slice(value.as_ref()), - value.start(), + self.locator.slice(expr.as_ref()), + expr.start(), ) { self.string_lines.push(line.start()); } diff --git a/crates/ruff_linter/src/docstrings/extraction.rs b/crates/ruff_linter/src/docstrings/extraction.rs index c0ca748145feb..a0597b06269b6 100644 --- a/crates/ruff_linter/src/docstrings/extraction.rs +++ b/crates/ruff_linter/src/docstrings/extraction.rs @@ -1,24 +1,23 @@ //! Extract docstrings from an AST. -use ruff_python_ast::{self as ast, Expr, Stmt}; +use ruff_python_ast::{self as ast, Stmt}; use ruff_python_semantic::{Definition, DefinitionId, Definitions, Member, MemberKind}; /// Extract a docstring from a function or class body. -pub(crate) fn docstring_from(suite: &[Stmt]) -> Option<&Expr> { +pub(crate) fn docstring_from(suite: &[Stmt]) -> Option<&ast::ExprStringLiteral> { let stmt = suite.first()?; // Require the docstring to be a standalone expression. let Stmt::Expr(ast::StmtExpr { value, range: _ }) = stmt else { return None; }; // Only match strings. - if !value.is_string_literal_expr() { - return None; - } - Some(value) + value.as_string_literal_expr() } /// Extract a docstring from a `Definition`. -pub(crate) fn extract_docstring<'a>(definition: &'a Definition<'a>) -> Option<&'a Expr> { +pub(crate) fn extract_docstring<'a>( + definition: &'a Definition<'a>, +) -> Option<&'a ast::ExprStringLiteral> { match definition { Definition::Module(module) => docstring_from(module.python_ast), Definition::Member(member) => docstring_from(member.body()), diff --git a/crates/ruff_linter/src/docstrings/mod.rs b/crates/ruff_linter/src/docstrings/mod.rs index aea8b14faa553..7715beea865ba 100644 --- a/crates/ruff_linter/src/docstrings/mod.rs +++ b/crates/ruff_linter/src/docstrings/mod.rs @@ -1,7 +1,7 @@ use std::fmt::{Debug, Formatter}; use std::ops::Deref; -use ruff_python_ast::Expr; +use ruff_python_ast::ExprStringLiteral; use ruff_python_semantic::Definition; use ruff_text_size::{Ranged, TextRange}; @@ -14,7 +14,8 @@ pub(crate) mod styles; #[derive(Debug)] pub(crate) struct Docstring<'a> { pub(crate) definition: &'a Definition<'a>, - pub(crate) expr: &'a Expr, + /// The literal AST node representing the docstring. + pub(crate) expr: &'a ExprStringLiteral, /// The content of the docstring, including the leading and trailing quotes. pub(crate) contents: &'a str, /// The range of the docstring body (without the quotes). The range is relative to [`Self::contents`]. diff --git a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs index ac8090395b776..4fadf908a3f01 100644 --- a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs +++ b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs @@ -1,7 +1,6 @@ -use ruff_text_size::TextRange; - use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::ExprStringLiteral; /// ## What it does /// Checks for hardcoded bindings to all network interfaces (`0.0.0.0`). @@ -35,9 +34,9 @@ impl Violation for HardcodedBindAllInterfaces { } /// S104 -pub(crate) fn hardcoded_bind_all_interfaces(value: &str, range: TextRange) -> Option { - if value == "0.0.0.0" { - Some(Diagnostic::new(HardcodedBindAllInterfaces, range)) +pub(crate) fn hardcoded_bind_all_interfaces(string: &ExprStringLiteral) -> Option { + if string.value == "0.0.0.0" { + Some(Diagnostic::new(HardcodedBindAllInterfaces, string.range)) } else { None } diff --git a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_tmp_directory.rs b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_tmp_directory.rs index 58e9180806ac5..1f5613647f54b 100644 --- a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_tmp_directory.rs +++ b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_tmp_directory.rs @@ -2,7 +2,6 @@ use ruff_python_ast::{self as ast, Expr}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_text_size::Ranged; use crate::checkers::ast::Checker; @@ -52,13 +51,13 @@ impl Violation for HardcodedTempFile { } /// S108 -pub(crate) fn hardcoded_tmp_directory(checker: &mut Checker, expr: &Expr, value: &str) { +pub(crate) fn hardcoded_tmp_directory(checker: &mut Checker, string: &ast::ExprStringLiteral) { if !checker .settings .flake8_bandit .hardcoded_tmp_directory .iter() - .any(|prefix| value.starts_with(prefix)) + .any(|prefix| string.value.starts_with(prefix)) { return; } @@ -77,8 +76,8 @@ pub(crate) fn hardcoded_tmp_directory(checker: &mut Checker, expr: &Expr, value: checker.diagnostics.push(Diagnostic::new( HardcodedTempFile { - string: value.to_string(), + string: string.value.clone(), }, - expr.range(), + string.range, )); } diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/docstring_in_stubs.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/docstring_in_stubs.rs index 38d06d0335c48..65c092a3f5471 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/docstring_in_stubs.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/docstring_in_stubs.rs @@ -1,4 +1,4 @@ -use ruff_python_ast::Expr; +use ruff_python_ast::ExprStringLiteral; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -36,8 +36,8 @@ impl Violation for DocstringInStub { } /// PYI021 -pub(crate) fn docstring_in_stubs(checker: &mut Checker, docstring: Option<&Expr>) { - if let Some(docstr) = &docstring { +pub(crate) fn docstring_in_stubs(checker: &mut Checker, docstring: Option<&ExprStringLiteral>) { + if let Some(docstr) = docstring { checker .diagnostics .push(Diagnostic::new(DocstringInStub, docstr.range())); diff --git a/crates/ruff_linter/src/rules/pydocstyle/helpers.rs b/crates/ruff_linter/src/rules/pydocstyle/helpers.rs index 9ce87aa2df033..355ff6ea16a8b 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/helpers.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/helpers.rs @@ -2,7 +2,6 @@ use std::collections::BTreeSet; use ruff_python_ast::call_path::from_qualified_name; use ruff_python_ast::helpers::map_callable; -use ruff_python_ast::Expr; use ruff_python_semantic::{Definition, SemanticModel}; use ruff_source_file::UniversalNewlines; @@ -61,13 +60,3 @@ pub(crate) fn should_ignore_definition( }) }) } - -/// Check if a docstring should be ignored. -pub(crate) fn should_ignore_docstring(docstring: &Expr) -> bool { - // Avoid analyzing docstrings that contain implicit string concatenations. - // Python does consider these docstrings, but they're almost certainly a - // user error, and supporting them "properly" is extremely difficult. - docstring - .as_string_literal_expr() - .is_some_and(|string_literal| string_literal.implicit_concatenated) -} diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs index e8de2983abcc8..7988490f6c18f 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs @@ -86,7 +86,7 @@ pub(crate) fn capitalized(checker: &mut Checker, docstring: &Docstring) { first_word: first_word.to_string(), capitalized_word: capitalized_word.to_string(), }, - docstring.expr.range(), + docstring.range(), ); diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs index b318127db94ac..12b37da83bc8f 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/newline_after_last_paragraph.rs @@ -96,8 +96,8 @@ pub(crate) fn newline_after_last_paragraph(checker: &mut Checker, docstring: &Do ); diagnostic.set_fix(Fix::safe_edit(Edit::replacement( content, - docstring.expr.end() - num_trailing_quotes - num_trailing_spaces, - docstring.expr.end() - num_trailing_quotes, + docstring.end() - num_trailing_quotes - num_trailing_spaces, + docstring.end() - num_trailing_quotes, ))); checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs b/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs index dcf30a2c43db2..7a536759967c5 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs @@ -71,13 +71,15 @@ fn as_literal(expr: &Expr) -> Option<&Expr> { } fn is_magic_value(expr: &Expr, allowed_types: &[ConstantType]) -> bool { - if let Ok(constant_type) = ConstantType::try_from(expr) { + if let Some(constant_type) = ConstantType::try_from_expr(expr) { if allowed_types.contains(&constant_type) { return false; } } match expr { + // Ignore `None`, `Bool`, and `Ellipsis` constants. + Expr::NoneLiteral(_) | Expr::BooleanLiteral(_) | Expr::EllipsisLiteral(_) => false, // Special-case some common string and integer types. Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => { !matches!(value.as_str(), "" | "__main__") diff --git a/crates/ruff_linter/src/rules/pylint/settings.rs b/crates/ruff_linter/src/rules/pylint/settings.rs index addbcfdfe3313..10187e39e878b 100644 --- a/crates/ruff_linter/src/rules/pylint/settings.rs +++ b/crates/ruff_linter/src/rules/pylint/settings.rs @@ -16,19 +16,17 @@ pub enum ConstantType { Str, } -impl TryFrom<&Expr> for ConstantType { - type Error = (); - - fn try_from(value: &Expr) -> Result { - match value { - Expr::StringLiteral(_) => Ok(Self::Str), - Expr::BytesLiteral(_) => Ok(Self::Bytes), +impl ConstantType { + pub fn try_from_expr(expr: &Expr) -> Option { + match expr { + Expr::StringLiteral(_) => Some(Self::Str), + Expr::BytesLiteral(_) => Some(Self::Bytes), Expr::NumberLiteral(ExprNumberLiteral { value, .. }) => match value { - Number::Int(_) => Ok(Self::Int), - Number::Float(_) => Ok(Self::Float), - Number::Complex { .. } => Ok(Self::Complex), + Number::Int(_) => Some(Self::Int), + Number::Float(_) => Some(Self::Float), + Number::Complex { .. } => Some(Self::Complex), }, - _ => Err(()), + _ => None, } } } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/unicode_kind_prefix.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/unicode_kind_prefix.rs index 20fcbf08a5913..13ecab830421d 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/unicode_kind_prefix.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/unicode_kind_prefix.rs @@ -1,6 +1,6 @@ use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::Expr; +use ruff_python_ast::ExprStringLiteral; use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::checkers::ast::Checker; @@ -39,11 +39,11 @@ impl AlwaysFixableViolation for UnicodeKindPrefix { } /// UP025 -pub(crate) fn unicode_kind_prefix(checker: &mut Checker, expr: &Expr, is_unicode: bool) { - if is_unicode { - let mut diagnostic = Diagnostic::new(UnicodeKindPrefix, expr.range()); +pub(crate) fn unicode_kind_prefix(checker: &mut Checker, string: &ExprStringLiteral) { + if string.unicode { + let mut diagnostic = Diagnostic::new(UnicodeKindPrefix, string.range); diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(TextRange::at( - expr.start(), + string.start(), TextSize::from(1), )))); checker.diagnostics.push(diagnostic); diff --git a/crates/ruff_linter/src/rules/refurb/rules/implicit_cwd.rs b/crates/ruff_linter/src/rules/refurb/rules/implicit_cwd.rs index 1ea75fbc9ad46..3b14b62b78009 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/implicit_cwd.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/implicit_cwd.rs @@ -63,10 +63,10 @@ pub(crate) fn no_implicit_cwd(checker: &mut Checker, call: &ExprCall) { [] => {} // Ex) `Path(".").resolve()` [arg] => { - let Expr::StringLiteral(ast::ExprStringLiteral { value: str, .. }) = arg else { + let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = arg else { return; }; - if !matches!(str.as_str(), "" | ".") { + if !matches!(value.as_str(), "" | ".") { return; } } diff --git a/crates/ruff_linter/src/rules/ruff/rules/invalid_index_type.rs b/crates/ruff_linter/src/rules/ruff/rules/invalid_index_type.rs index c3c3807a7da55..f67a52b7a45cd 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/invalid_index_type.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/invalid_index_type.rs @@ -149,7 +149,7 @@ pub(crate) fn invalid_index_type(checker: &mut Checker, expr: &ExprSubscript) { /// These are generally "literal" type expressions in that we know their concrete type /// without additional analysis; opposed to expressions like a function call where we /// cannot determine what type it may return. -#[derive(Debug, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] enum CheckableExprType { FString, StringLiteral, @@ -220,7 +220,7 @@ impl CheckableExprType { } } - fn is_literal(&self) -> bool { + fn is_literal(self) -> bool { matches!( self, Self::StringLiteral diff --git a/crates/ruff_python_codegen/src/generator.rs b/crates/ruff_python_codegen/src/generator.rs index 173b6832c5e22..fc57856633c18 100644 --- a/crates/ruff_python_codegen/src/generator.rs +++ b/crates/ruff_python_codegen/src/generator.rs @@ -1094,8 +1094,8 @@ impl<'a> Generator<'a> { self.p_bytes_repr(value); } Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => { + static INF_STR: &str = "1e309"; assert_eq!(f64::MAX_10_EXP, 308); - let inf_str = "1e309"; match value { ast::Number::Int(i) => { @@ -1103,7 +1103,7 @@ impl<'a> Generator<'a> { } ast::Number::Float(fp) => { if fp.is_infinite() { - self.p(inf_str); + self.p(INF_STR); } else { self.p(&ruff_python_literal::float::to_string(*fp)); } @@ -1115,7 +1115,7 @@ impl<'a> Generator<'a> { format!("({real}{imag:+}j)") }; if real.is_infinite() || imag.is_infinite() { - self.p(&value.replace("inf", inf_str)); + self.p(&value.replace("inf", INF_STR)); } else { self.p(&value); } diff --git a/crates/ruff_python_formatter/src/expression/expr_boolean_literal.rs b/crates/ruff_python_formatter/src/expression/expr_boolean_literal.rs index caddae80fa962..756df8dfd4ca7 100644 --- a/crates/ruff_python_formatter/src/expression/expr_boolean_literal.rs +++ b/crates/ruff_python_formatter/src/expression/expr_boolean_literal.rs @@ -1,7 +1,6 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprBooleanLiteral; -use crate::comments::SourceComment; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -16,14 +15,6 @@ impl FormatNodeRule for FormatExprBooleanLiteral { token("False").fmt(f) } } - - fn fmt_dangling_comments( - &self, - _dangling_comments: &[SourceComment], - _f: &mut PyFormatter, - ) -> FormatResult<()> { - Ok(()) - } } impl NeedsParentheses for ExprBooleanLiteral { diff --git a/crates/ruff_python_formatter/src/expression/expr_bytes_literal.rs b/crates/ruff_python_formatter/src/expression/expr_bytes_literal.rs index 0892380edd45d..968487a07cb4a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bytes_literal.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bytes_literal.rs @@ -1,32 +1,18 @@ -use ruff_formatter::FormatRuleWithOptions; use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprBytesLiteral; use crate::comments::SourceComment; use crate::expression::expr_string_literal::is_multiline_string; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; -use crate::expression::string::{AnyString, FormatString, StringLayout}; +use crate::expression::string::{AnyString, FormatString}; use crate::prelude::*; #[derive(Default)] -pub struct FormatExprBytesLiteral { - layout: StringLayout, -} - -impl FormatRuleWithOptions> for FormatExprBytesLiteral { - type Options = StringLayout; - - fn with_options(mut self, options: Self::Options) -> Self { - self.layout = options; - self - } -} +pub struct FormatExprBytesLiteral; impl FormatNodeRule for FormatExprBytesLiteral { fn fmt_fields(&self, item: &ExprBytesLiteral, f: &mut PyFormatter) -> FormatResult<()> { - FormatString::new(&AnyString::Bytes(item)) - .with_layout(self.layout) - .fmt(f) + FormatString::new(&AnyString::Bytes(item)).fmt(f) } fn fmt_dangling_comments( @@ -34,6 +20,7 @@ impl FormatNodeRule for FormatExprBytesLiteral { _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { + // Handled as part of `fmt_fields` Ok(()) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_ellipsis_literal.rs b/crates/ruff_python_formatter/src/expression/expr_ellipsis_literal.rs index 70472d4feed37..2eb26787c6f73 100644 --- a/crates/ruff_python_formatter/src/expression/expr_ellipsis_literal.rs +++ b/crates/ruff_python_formatter/src/expression/expr_ellipsis_literal.rs @@ -1,7 +1,6 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprEllipsisLiteral; -use crate::comments::SourceComment; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -12,14 +11,6 @@ impl FormatNodeRule for FormatExprEllipsisLiteral { fn fmt_fields(&self, _item: &ExprEllipsisLiteral, f: &mut PyFormatter) -> FormatResult<()> { token("...").fmt(f) } - - fn fmt_dangling_comments( - &self, - _dangling_comments: &[SourceComment], - _f: &mut PyFormatter, - ) -> FormatResult<()> { - Ok(()) - } } impl NeedsParentheses for ExprEllipsisLiteral { diff --git a/crates/ruff_python_formatter/src/expression/expr_none_literal.rs b/crates/ruff_python_formatter/src/expression/expr_none_literal.rs index 0367015a14712..db08cb3f32dca 100644 --- a/crates/ruff_python_formatter/src/expression/expr_none_literal.rs +++ b/crates/ruff_python_formatter/src/expression/expr_none_literal.rs @@ -1,7 +1,6 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprNoneLiteral; -use crate::comments::SourceComment; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -12,14 +11,6 @@ impl FormatNodeRule for FormatExprNoneLiteral { fn fmt_fields(&self, _item: &ExprNoneLiteral, f: &mut PyFormatter) -> FormatResult<()> { token("None").fmt(f) } - - fn fmt_dangling_comments( - &self, - _dangling_comments: &[SourceComment], - _f: &mut PyFormatter, - ) -> FormatResult<()> { - Ok(()) - } } impl NeedsParentheses for ExprNoneLiteral { diff --git a/crates/ruff_python_formatter/src/expression/expr_number_literal.rs b/crates/ruff_python_formatter/src/expression/expr_number_literal.rs index 2cdaded4d49ff..4559336392bb0 100644 --- a/crates/ruff_python_formatter/src/expression/expr_number_literal.rs +++ b/crates/ruff_python_formatter/src/expression/expr_number_literal.rs @@ -1,7 +1,6 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::{ExprNumberLiteral, Number}; -use crate::comments::SourceComment; use crate::expression::number::{FormatComplex, FormatFloat, FormatInt}; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -17,14 +16,6 @@ impl FormatNodeRule for FormatExprNumberLiteral { Number::Complex { .. } => FormatComplex::new(item).fmt(f), } } - - fn fmt_dangling_comments( - &self, - _dangling_comments: &[SourceComment], - _f: &mut PyFormatter, - ) -> FormatResult<()> { - Ok(()) - } } impl NeedsParentheses for ExprNumberLiteral { diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index 7af61b4fbd06e..fa50c8ba42df9 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -500,7 +500,7 @@ pub(crate) fn contains_only_an_ellipsis(body: &[Stmt], comments: &Comments) -> b let [node] = body else { return false; }; - matches!(value.as_ref(), Expr::EllipsisLiteral(_)) && !comments.has_leading(node) + value.is_ellipsis_literal_expr() && !comments.has_leading(node) } _ => false, }