diff --git a/crates/ruff/src/commands/check.rs b/crates/ruff/src/commands/check.rs index 18101d7757a99..d62a93426ba7f 100644 --- a/crates/ruff/src/commands/check.rs +++ b/crates/ruff/src/commands/check.rs @@ -252,6 +252,7 @@ mod test { for file in [&pyproject_toml, &python_file, ¬ebook] { fs::OpenOptions::new() .create(true) + .truncate(true) .write(true) .mode(0o000) .open(file)?; diff --git a/crates/ruff/src/panic.rs b/crates/ruff/src/panic.rs index d7ab8d38a90f3..5947873ef775d 100644 --- a/crates/ruff/src/panic.rs +++ b/crates/ruff/src/panic.rs @@ -16,7 +16,7 @@ impl std::fmt::Display for PanicError { } thread_local! { - static LAST_PANIC: std::cell::Cell> = std::cell::Cell::new(None); + static LAST_PANIC: std::cell::Cell> = const { std::cell::Cell::new(None) }; } /// [`catch_unwind`](std::panic::catch_unwind) wrapper that sets a custom [`set_hook`](std::panic::set_hook) diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index 84bcd3d1b6a02..e496a51a1e89d 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -1353,6 +1353,7 @@ fn unreadable_pyproject_toml() -> Result<()> { // Create an empty file with 000 permissions fs::OpenOptions::new() .create(true) + .truncate(true) .write(true) .mode(0o000) .open(pyproject_toml)?; diff --git a/crates/ruff_linter/src/checkers/imports.rs b/crates/ruff_linter/src/checkers/imports.rs index 5ecb477d85e29..41294eb57055e 100644 --- a/crates/ruff_linter/src/checkers/imports.rs +++ b/crates/ruff_linter/src/checkers/imports.rs @@ -20,12 +20,7 @@ use crate::rules::isort::block::{Block, BlockBuilder}; use crate::settings::LinterSettings; fn extract_import_map(path: &Path, package: Option<&Path>, blocks: &[&Block]) -> Option { - let Some(package) = package else { - return None; - }; - let Some(module_path) = to_module_path(package, path) else { - return None; - }; + let module_path = to_module_path(package?, path)?; let num_imports = blocks.iter().map(|block| block.imports.len()).sum(); let mut module_imports = Vec::with_capacity(num_imports); diff --git a/crates/ruff_linter/src/fix/edits.rs b/crates/ruff_linter/src/fix/edits.rs index 3e4ac1f3170e8..37b8c76569d3d 100644 --- a/crates/ruff_linter/src/fix/edits.rs +++ b/crates/ruff_linter/src/fix/edits.rs @@ -40,10 +40,7 @@ pub(crate) fn delete_stmt( locator: &Locator, indexer: &Indexer, ) -> Edit { - if parent - .map(|parent| is_lone_child(stmt, parent)) - .unwrap_or_default() - { + if parent.is_some_and(|parent| is_lone_child(stmt, parent)) { // If removing this node would lead to an invalid syntax tree, replace // it with a `pass`. Edit::range_replacement("pass".to_string(), stmt.range()) diff --git a/crates/ruff_linter/src/importer/insertion.rs b/crates/ruff_linter/src/importer/insertion.rs index 15a46c6f0215d..5cd6ae200c21c 100644 --- a/crates/ruff_linter/src/importer/insertion.rs +++ b/crates/ruff_linter/src/importer/insertion.rs @@ -278,9 +278,7 @@ impl<'a> Insertion<'a> { /// Find the end of the last docstring. fn match_docstring_end(body: &[Stmt]) -> Option { let mut iter = body.iter(); - let Some(mut stmt) = iter.next() else { - return None; - }; + let mut stmt = iter.next()?; if !is_docstring_stmt(stmt) { return None; } diff --git a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_password_string.rs b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_password_string.rs index 07cbea8c9fbc6..13e74cabeafb0 100644 --- a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_password_string.rs +++ b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_password_string.rs @@ -80,9 +80,7 @@ pub(crate) fn compare_to_hardcoded_password_string( .diagnostics .extend(comparators.iter().filter_map(|comp| { string_literal(comp).filter(|string| !string.is_empty())?; - let Some(name) = password_target(left) else { - return None; - }; + let name = password_target(left)?; Some(Diagnostic::new( HardcodedPasswordString { name: name.to_string(), diff --git a/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs b/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs index 980cdf00d6134..7b5596d55269b 100644 --- a/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs +++ b/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs @@ -159,9 +159,7 @@ fn get_element_type(element: &Stmt, semantic: &SemanticModel) -> Option, ) -> Option { - let Some(import) = binding.as_any_import() else { - return None; - }; - + let import = binding.as_any_import()?; let qualified_name = import.qualified_name().to_string(); - - let Some(expected_alias) = conventions.get(qualified_name.as_str()) else { - return None; - }; + let expected_alias = conventions.get(qualified_name.as_str())?; let name = binding.name(checker.locator()); if binding.is_alias() && name == expected_alias { diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_literal_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_literal_union.rs index 56808290f4415..4d9c5f9d503b9 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_literal_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_literal_union.rs @@ -148,10 +148,7 @@ fn match_builtin_type(expr: &Expr, semantic: &SemanticModel) -> Option /// Return the [`ExprType`] of an [`Expr`] if it is a literal (e.g., an `int`, like `1`, or a /// `bool`, like `True`). fn match_literal_type(expr: &Expr) -> Option { - let Some(literal_expr) = expr.as_literal_expr() else { - return None; - }; - let result = match literal_expr { + Some(match expr.as_literal_expr()? { LiteralExpressionRef::BooleanLiteral(_) => ExprType::Bool, LiteralExpressionRef::StringLiteral(_) => ExprType::Str, LiteralExpressionRef::BytesLiteral(_) => ExprType::Bytes, @@ -163,6 +160,5 @@ fn match_literal_type(expr: &Expr) -> Option { LiteralExpressionRef::NoneLiteral(_) | LiteralExpressionRef::EllipsisLiteral(_) => { return None; } - }; - Some(result) + }) } diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs index 664dd8878eeeb..8e4c1b28ca8e6 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs @@ -8,9 +8,7 @@ pub(super) fn get_mark_decorators( decorators: &[Decorator], ) -> impl Iterator { decorators.iter().filter_map(|decorator| { - let Some(name) = UnqualifiedName::from_expr(map_callable(&decorator.expression)) else { - return None; - }; + let name = UnqualifiedName::from_expr(map_callable(&decorator.expression))?; let ["pytest", "mark", marker] = name.segments() else { return None; }; diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs index 88a08dd38e6eb..04ed8b05a5dbd 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -541,7 +541,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) { // Create a `x in (a, b)` expression. let node = ast::ExprTuple { - elts: comparators.into_iter().map(Clone::clone).collect(), + elts: comparators.into_iter().cloned().collect(), ctx: ExprContext::Load, range: TextRange::default(), parenthesized: true, diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/reimplemented_builtin.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/reimplemented_builtin.rs index 7dd5cc5d1419a..3266f0f2ab675 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/reimplemented_builtin.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/reimplemented_builtin.rs @@ -274,10 +274,11 @@ fn match_loop(stmt: &Stmt) -> Option { if !nested_elif_else_clauses.is_empty() { return None; } - let [Stmt::Return(ast::StmtReturn { value, range: _ })] = nested_body.as_slice() else { - return None; - }; - let Some(value) = value else { + let [Stmt::Return(ast::StmtReturn { + value: Some(value), + range: _, + })] = nested_body.as_slice() + else { return None; }; let Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) = value.as_ref() else { diff --git a/crates/ruff_linter/src/rules/flake8_tidy_imports/rules/relative_imports.rs b/crates/ruff_linter/src/rules/flake8_tidy_imports/rules/relative_imports.rs index 4a7420f9a67ad..20c319b07fd4c 100644 --- a/crates/ruff_linter/src/rules/flake8_tidy_imports/rules/relative_imports.rs +++ b/crates/ruff_linter/src/rules/flake8_tidy_imports/rules/relative_imports.rs @@ -82,9 +82,7 @@ fn fix_banned_relative_import( generator: Generator, ) -> Option { // Only fix is the module path is known. - let Some(module_path) = resolve_imported_module_path(level, module, module_path) else { - return None; - }; + let module_path = resolve_imported_module_path(level, module, module_path)?; // Require import to be a valid module: // https://python.org/dev/peps/pep-0008/#package-and-module-names diff --git a/crates/ruff_linter/src/rules/isort/categorize.rs b/crates/ruff_linter/src/rules/isort/categorize.rs index f7fe77dd71138..874070135c21e 100644 --- a/crates/ruff_linter/src/rules/isort/categorize.rs +++ b/crates/ruff_linter/src/rules/isort/categorize.rs @@ -80,10 +80,12 @@ enum Reason<'a> { Future, KnownStandardLibrary, SamePackage, + #[allow(dead_code)] SourceMatch(&'a Path), NoMatch, UserDefinedSection, NoSections, + #[allow(dead_code)] DisabledSection(&'a ImportSection), } diff --git a/crates/ruff_linter/src/rules/isort/sorting.rs b/crates/ruff_linter/src/rules/isort/sorting.rs index aa979fc90c89e..68d899bc61a99 100644 --- a/crates/ruff_linter/src/rules/isort/sorting.rs +++ b/crates/ruff_linter/src/rules/isort/sorting.rs @@ -103,9 +103,7 @@ impl<'a> ModuleKey<'a> { ) -> Self { let level = level.unwrap_or_default(); - let force_to_top = !name - .map(|name| settings.force_to_top.contains(name)) - .unwrap_or_default(); // `false` < `true` so we get forced to top first + let force_to_top = !name.is_some_and(|name| settings.force_to_top.contains(name)); // `false` < `true` so we get forced to top first let maybe_length = (settings.length_sort || (settings.length_sort_straight && style == ImportStyle::Straight)) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/doc_line_too_long.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/doc_line_too_long.rs index f3c5fe4f252ff..b13c461e19fc2 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/doc_line_too_long.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/doc_line_too_long.rs @@ -87,10 +87,7 @@ pub(crate) fn doc_line_too_long( indexer: &Indexer, settings: &LinterSettings, ) -> Option { - let Some(limit) = settings.pycodestyle.max_doc_length else { - return None; - }; - + let limit = settings.pycodestyle.max_doc_length?; Overlong::try_from_line( line, indexer, diff --git a/crates/ruff_linter/src/rules/pylint/rules/import_self.rs b/crates/ruff_linter/src/rules/pylint/rules/import_self.rs index 641aeea887118..f341b8d7f3a9f 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/import_self.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/import_self.rs @@ -35,9 +35,7 @@ impl Violation for ImportSelf { /// PLW0406 pub(crate) fn import_self(alias: &Alias, module_path: Option<&[String]>) -> Option { - let Some(module_path) = module_path else { - return None; - }; + let module_path = module_path?; if alias.name.split('.').eq(module_path) { return Some(Diagnostic::new( @@ -58,13 +56,8 @@ pub(crate) fn import_from_self( names: &[Alias], module_path: Option<&[String]>, ) -> Option { - let Some(module_path) = module_path else { - return None; - }; - let Some(imported_module_path) = resolve_imported_module_path(level, module, Some(module_path)) - else { - return None; - }; + let module_path = module_path?; + let imported_module_path = resolve_imported_module_path(level, module, Some(module_path))?; if imported_module_path .split('.') diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs index e01817c6b6bc1..a8a520e90351d 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs @@ -86,9 +86,7 @@ impl<'a> FormatSummaryValues<'a> { value, range: _, } = keyword; - let Some(key) = arg else { - return None; - }; + let key = arg.as_ref()?; if contains_quotes(locator.slice(value)) || locator.contains_line_break(value.range()) { return None; } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs index 8e738d39e5ae6..11b2aedbd86a0 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs @@ -260,9 +260,7 @@ fn clean_params_dictionary(right: &Expr, locator: &Locator, stylist: &Stylist) - } contents.push('('); if is_multi_line { - let Some(indent) = indent else { - return None; - }; + let indent = indent?; for item in &arguments { contents.push_str(stylist.line_ending().as_str()); diff --git a/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs b/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs index 50945f5b980c8..8fe4215551b6f 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/invalid_formatter_suppression_comment.rs @@ -127,8 +127,7 @@ impl<'src, 'loc> UselessSuppressionComments<'src, 'loc> { // check if the comment is inside of an expression. if comment .enclosing - .map(|n| !is_valid_enclosing_node(n)) - .unwrap_or_default() + .is_some_and(|n| !is_valid_enclosing_node(n)) { return Err(IgnoredReason::InNonStatement); } diff --git a/crates/ruff_linter/src/rules/ruff/rules/pairwise_over_zipped.rs b/crates/ruff_linter/src/rules/ruff/rules/pairwise_over_zipped.rs index 9f4bed6e4df66..f6a0c674c178f 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/pairwise_over_zipped.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/pairwise_over_zipped.rs @@ -83,12 +83,7 @@ fn match_slice_info(expr: &Expr) -> Option { else { return None; }; - - let Some(slice_start) = int.as_i32() else { - return None; - }; - - Some(slice_start) + Some(int.as_i32()?) } else { None }; diff --git a/crates/ruff_linter/src/test.rs b/crates/ruff_linter/src/test.rs index 40b014da502c5..ff9cf99da21ae 100644 --- a/crates/ruff_linter/src/test.rs +++ b/crates/ruff_linter/src/test.rs @@ -92,7 +92,7 @@ pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec { } thread_local! { - static MAX_ITERATIONS: std::cell::Cell = std::cell::Cell::new(8); + static MAX_ITERATIONS: std::cell::Cell = const { std::cell::Cell::new(8) }; } pub fn set_max_iterations(max: usize) { diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 7a198855fe25e..72592b935679e 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -878,9 +878,7 @@ pub fn resolve_imported_module_path<'a>( return Some(Cow::Borrowed(module.unwrap_or(""))); } - let Some(module_path) = module_path else { - return None; - }; + let module_path = module_path?; if level as usize >= module_path.len() { return None; diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs index 1cd449c4e6825..a19a6a7bc004e 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs @@ -175,9 +175,7 @@ fn find_double_star(pattern: &PatternMatchMapping, source: &str) -> Option<(Text } = pattern; // If there's no `rest` element, there's no `**`. - let Some(rest) = rest else { - return None; - }; + let rest = rest.as_ref()?; let mut tokenizer = SimpleTokenizer::starts_at(patterns.last().map_or(pattern.start(), Ranged::end), source); diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 83a52c3838614..fcc85b9ecbf95 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "1.76" +channel = "1.77"