Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,7 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
);
}
if checker.is_rule_enabled(Rule::UnnecessaryLiteralWithinTupleCall) {
flake8_comprehensions::rules::unnecessary_literal_within_tuple_call(
checker, expr, call,
);
flake8_comprehensions::rules::unnecessary_literal_within_tuple_call(checker, call);
}
if checker.is_rule_enabled(Rule::UnnecessaryLiteralWithinListCall) {
flake8_comprehensions::rules::unnecessary_literal_within_list_call(checker, call);
Expand Down
7 changes: 0 additions & 7 deletions crates/ruff_linter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ pub(crate) const fn is_comprehension_with_min_max_sum_enabled(settings: &LinterS
settings.preview.is_enabled()
}

// https://github.com/astral-sh/ruff/pull/12657
pub(crate) const fn is_check_comprehensions_in_tuple_call_enabled(
settings: &LinterSettings,
) -> bool {
settings.preview.is_enabled()
}

// https://github.com/astral-sh/ruff/issues/15347
pub(crate) const fn is_bad_version_info_in_non_stub_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
Expand Down
26 changes: 2 additions & 24 deletions crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ mod tests {
use ruff_python_ast::PythonVersion;
use test_case::test_case;

use crate::assert_diagnostics;
use crate::registry::Rule;
use crate::settings::LinterSettings;
use crate::settings::types::PreviewMode;
use crate::test::test_path;
use crate::{assert_diagnostics, assert_diagnostics_diff};

#[test_case(Rule::UnnecessaryCallAroundSorted, Path::new("C413.py"))]
#[test_case(Rule::UnnecessaryCollectionCall, Path::new("C408.py"))]
Expand Down Expand Up @@ -57,6 +57,7 @@ mod tests {
#[test_case(Rule::UnnecessaryListComprehensionSet, Path::new("C403_py315.py"))]
#[test_case(Rule::UnnecessaryListCall, Path::new("C411_py315.py"))]
#[test_case(Rule::UnnecessaryLiteralWithinDictCall, Path::new("C418_py315.py"))]
#[test_case(Rule::UnnecessaryLiteralWithinTupleCall, Path::new("C409_py315.py"))]
#[test_case(Rule::UnnecessaryComprehensionInCall, Path::new("C419_py315.py"))]
fn rules_py315(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
Expand All @@ -68,7 +69,6 @@ mod tests {
Ok(())
}

#[test_case(Rule::UnnecessaryLiteralWithinTupleCall, Path::new("C409.py"))]
#[test_case(Rule::UnnecessaryComprehensionInCall, Path::new("C419_1.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
Expand All @@ -87,28 +87,6 @@ mod tests {
Ok(())
}

#[test_case(Rule::UnnecessaryLiteralWithinTupleCall, Path::new("C409_py315.py"))]
fn preview_rules_py315(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
assert_diagnostics_diff!(
snapshot,
Path::new("flake8_comprehensions").join(path).as_path(),
&LinterSettings {
preview: PreviewMode::Disabled,
..LinterSettings::for_rule(rule_code).with_target_version(PythonVersion::PY315)
},
&LinterSettings {
preview: PreviewMode::Enabled,
..LinterSettings::for_rule(rule_code).with_target_version(PythonVersion::PY315)
},
);
Ok(())
}

#[test_case(Rule::UnnecessaryCollectionCall, Path::new("C408.py"))]
fn allow_dict_calls_with_keyword_arguments(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::helpers::any_over_expr;
use ruff_python_ast::{self as ast, Expr};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange, TextSize};

use crate::checkers::ast::Checker;
use crate::preview::is_check_comprehensions_in_tuple_call_enabled;
use crate::rules::flake8_comprehensions::fixes;
use crate::{Edit, Fix, FixAvailability, Violation};

use crate::rules::flake8_comprehensions::helpers;

/// ## What it does
/// Checks for `tuple` calls that take unnecessary list or tuple literals as
/// arguments. In [preview], this also includes unnecessary list comprehensions
/// within tuple calls.
/// arguments.
///
/// ## Why is this bad?
/// It's unnecessary to use a list or tuple literal within a `tuple()` call,
Expand All @@ -24,29 +20,21 @@ use crate::rules::flake8_comprehensions::helpers;
/// literal. Otherwise, if a tuple literal was passed, then the outer call
/// to `tuple()` should be removed.
///
/// In [preview], this rule also checks for list comprehensions within `tuple()`
/// calls. If a list comprehension is found, it should be rewritten as a
/// generator expression.
///
/// ## Example
/// ```python
/// tuple([1, 2])
/// tuple((1, 2))
/// tuple([x for x in range(10)])
/// ```
///
/// Use instead:
/// ```python
/// (1, 2)
/// (1, 2)
/// tuple(x for x in range(10))
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as unsafe, as it may occasionally drop comments
/// when rewriting the call. In most cases, though, comments will be preserved.
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[derive(ViolationMetadata)]
#[violation_metadata(stable_since = "v0.0.66")]
pub(crate) struct UnnecessaryLiteralWithinTupleCall {
Expand All @@ -67,29 +55,20 @@ impl Violation for UnnecessaryLiteralWithinTupleCall {
"Unnecessary tuple literal passed to `tuple()` (remove the outer call to `tuple()`)"
.to_string()
}
TupleLiteralKind::ListComp => {
"Unnecessary list comprehension passed to `tuple()` (rewrite as a generator)"
.to_string()
}
}
}

fn fix_title(&self) -> Option<String> {
let title = match self.literal_kind {
TupleLiteralKind::List => "Rewrite as a tuple literal",
TupleLiteralKind::Tuple => "Remove the outer call to `tuple()`",
TupleLiteralKind::ListComp => "Rewrite as a generator",
};
Some(title.to_string())
}
}

/// C409
pub(crate) fn unnecessary_literal_within_tuple_call(
checker: &Checker,
expr: &Expr,
call: &ast::ExprCall,
) {
pub(crate) fn unnecessary_literal_within_tuple_call(checker: &Checker, call: &ast::ExprCall) {
if !call.arguments.keywords.is_empty() {
return;
}
Expand All @@ -104,9 +83,6 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
let argument_kind = match argument {
Expr::Tuple(_) => TupleLiteralKind::Tuple,
Expr::List(_) => TupleLiteralKind::List,
Expr::ListComp(_) if is_check_comprehensions_in_tuple_call_enabled(checker.settings()) => {
TupleLiteralKind::ListComp
}
_ => return,
};
if !checker.semantic().has_builtin_binding("tuple") {
Expand Down Expand Up @@ -154,24 +130,6 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
});
}

Expr::ListComp(ast::ExprListComp { elt, .. }) => {
if any_over_expr(elt, &Expr::is_await_expr) {
return;
}
if elt.is_starred_expr() {
// The LibCST-based fixer does not yet support PEP 798 unpacking comprehensions.
return;
}
// Convert `tuple([x for x in range(10)])` to `tuple(x for x in range(10))`
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_comprehension_in_call(
expr,
checker.locator(),
checker.stylist(),
)
});
}

_ => (),
}
}
Expand All @@ -180,5 +138,4 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
enum TupleLiteralKind {
List,
Tuple,
ListComp,
}
Comment thread
ntBre marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---

Loading
Loading