From 32b57b2ee47af5c1b446b344d9611ce108f07ecc Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 25 Oct 2024 09:46:30 +0200 Subject: [PATCH] Enable nursery rules: 'redundant_clone', 'debug_assert_with_mut_call', and 'unused_peekable' (#13920) --- Cargo.toml | 4 ++++ crates/red_knot/src/main.rs | 2 +- crates/red_knot_python_semantic/src/ast_node_ref.rs | 4 ++-- .../src/module_resolver/resolver.rs | 4 ++-- .../src/semantic_index/definition.rs | 2 +- .../red_knot_python_semantic/src/util/subscript.rs | 1 + .../server/api/notifications/did_open_notebook.rs | 2 +- crates/red_knot_wasm/src/lib.rs | 4 ++-- .../src/rules/eradicate/rules/commented_out_code.rs | 2 +- .../src/rules/pydocstyle/rules/sections.rs | 2 +- .../ruff_linter/src/rules/ruff/rules/test_rules.rs | 12 +++--------- crates/ruff_macros/src/config.rs | 6 ++---- crates/ruff_python_trivia/src/tokenizer.rs | 5 ++++- crates/ruff_server/tests/notebook.rs | 2 +- crates/ruff_workspace/src/configuration.rs | 4 ++-- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b52dc0249e..681a20eb9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -202,6 +202,10 @@ get_unwrap = "warn" rc_buffer = "warn" rc_mutex = "warn" rest_pat_in_fully_bound_structs = "warn" +# nursery rules +redundant_clone = "warn" +debug_assert_with_mut_call = "warn" +unused_peekable = "warn" [profile.release] # Note that we set these explicitly, and these values diff --git a/crates/red_knot/src/main.rs b/crates/red_knot/src/main.rs index 2dd24e5a6a..2244e5df2d 100644 --- a/crates/red_knot/src/main.rs +++ b/crates/red_knot/src/main.rs @@ -144,7 +144,7 @@ pub fn main() -> ExitStatus { } fn run() -> anyhow::Result { - let args = Args::parse_from(std::env::args().collect::>()); + let args = Args::parse_from(std::env::args()); if matches!(args.command, Some(Command::Server)) { return run_server().map(|()| ExitStatus::Success); diff --git a/crates/red_knot_python_semantic/src/ast_node_ref.rs b/crates/red_knot_python_semantic/src/ast_node_ref.rs index 6ea0267c0b..367748b5ad 100644 --- a/crates/red_knot_python_semantic/src/ast_node_ref.rs +++ b/crates/red_knot_python_semantic/src/ast_node_ref.rs @@ -132,7 +132,7 @@ mod tests { #[test] fn inequality() { let parsed_raw = parse_unchecked_source("1 + 2", PySourceType::Python); - let parsed = ParsedModule::new(parsed_raw.clone()); + let parsed = ParsedModule::new(parsed_raw); let stmt = &parsed.syntax().body[0]; let node = unsafe { AstNodeRef::new(parsed.clone(), stmt) }; @@ -150,7 +150,7 @@ mod tests { #[allow(unsafe_code)] fn debug() { let parsed_raw = parse_unchecked_source("1 + 2", PySourceType::Python); - let parsed = ParsedModule::new(parsed_raw.clone()); + let parsed = ParsedModule::new(parsed_raw); let stmt = &parsed.syntax().body[0]; diff --git a/crates/red_knot_python_semantic/src/module_resolver/resolver.rs b/crates/red_knot_python_semantic/src/module_resolver/resolver.rs index a51bee3065..808dbab0fa 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/resolver.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/resolver.rs @@ -1294,7 +1294,7 @@ mod tests { search_paths: SearchPathSettings { extra_paths: vec![], src_root: src.clone(), - custom_typeshed: Some(custom_typeshed.clone()), + custom_typeshed: Some(custom_typeshed), site_packages: SitePackages::Known(vec![site_packages]), }, }, @@ -1445,7 +1445,7 @@ mod tests { assert_function_query_was_not_run( &db, resolve_module_query, - ModuleNameIngredient::new(&db, functools_module_name.clone()), + ModuleNameIngredient::new(&db, functools_module_name), &events, ); assert_eq!(functools_module.search_path(), &stdlib); diff --git a/crates/red_knot_python_semantic/src/semantic_index/definition.rs b/crates/red_knot_python_semantic/src/semantic_index/definition.rs index 3b1b284ad7..4da4c4e6a7 100644 --- a/crates/red_knot_python_semantic/src/semantic_index/definition.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/definition.rs @@ -296,7 +296,7 @@ impl DefinitionNodeRef<'_> { handler, is_star, }) => DefinitionKind::ExceptHandler(ExceptHandlerDefinitionKind { - handler: AstNodeRef::new(parsed.clone(), handler), + handler: AstNodeRef::new(parsed, handler), is_star, }), } diff --git a/crates/red_knot_python_semantic/src/util/subscript.rs b/crates/red_knot_python_semantic/src/util/subscript.rs index 8d72cbe424..a88276e4b4 100644 --- a/crates/red_knot_python_semantic/src/util/subscript.rs +++ b/crates/red_knot_python_semantic/src/util/subscript.rs @@ -18,6 +18,7 @@ impl> PythonSubscript for T { } #[cfg(test)] +#[allow(clippy::redundant_clone)] mod tests { use super::PythonSubscript; diff --git a/crates/red_knot_server/src/server/api/notifications/did_open_notebook.rs b/crates/red_knot_server/src/server/api/notifications/did_open_notebook.rs index acd1c3ea30..edbff7e022 100644 --- a/crates/red_knot_server/src/server/api/notifications/did_open_notebook.rs +++ b/crates/red_knot_server/src/server/api/notifications/did_open_notebook.rs @@ -37,7 +37,7 @@ impl SyncNotificationHandler for DidOpenNotebookHandler { params.cell_text_documents, ) .with_failure_code(ErrorCode::InternalError)?; - session.open_notebook_document(params.notebook_document.uri.clone(), notebook); + session.open_notebook_document(params.notebook_document.uri, notebook); match path { AnySystemPath::System(path) => { diff --git a/crates/red_knot_wasm/src/lib.rs b/crates/red_knot_wasm/src/lib.rs index 0379ad3fdd..2d70b3d684 100644 --- a/crates/red_knot_wasm/src/lib.rs +++ b/crates/red_knot_wasm/src/lib.rs @@ -110,14 +110,14 @@ impl Workspace { pub fn check_file(&self, file_id: &FileHandle) -> Result, Error> { let result = self.db.check_file(file_id.file).map_err(into_error)?; - Ok(result.clone()) + Ok(result) } /// Checks all open files pub fn check(&self) -> Result, Error> { let result = self.db.check().map_err(into_error)?; - Ok(result.clone()) + Ok(result) } /// Returns the parsed AST for `path` diff --git a/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs b/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs index b87dd9b8bf..53d87dcf94 100644 --- a/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs +++ b/crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs @@ -88,7 +88,7 @@ where let line_end = locator.full_line_end(script_start.end()); let rest = locator.after(line_end); let mut end_offset = None; - let mut lines = UniversalNewlineIterator::with_offset(rest, line_end).peekable(); + let mut lines = UniversalNewlineIterator::with_offset(rest, line_end); while let Some(line) = lines.next() { let Some(content) = script_line_content(&line) else { diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs index e18d7948ea..647e53a24e 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs @@ -1850,7 +1850,7 @@ static GOOGLE_ARGS_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"^\s*(\*?\*?\w+)\s*(\(.*?\))?\s*:(\r\n|\n)?\s*.+").unwrap()); fn args_section(context: &SectionContext) -> FxHashSet { - let mut following_lines = context.following_lines().peekable(); + let mut following_lines = context.following_lines(); let Some(first_line) = following_lines.next() else { return FxHashSet::default(); }; diff --git a/crates/ruff_linter/src/rules/ruff/rules/test_rules.rs b/crates/ruff_linter/src/rules/ruff/rules/test_rules.rs index fb2a5da227..a3f2548738 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/test_rules.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/test_rules.rs @@ -122,10 +122,7 @@ impl TestRule for StableTestRuleSafeFix { } else { Some( Diagnostic::new(StableTestRuleSafeFix, ruff_text_size::TextRange::default()) - .with_fix(Fix::safe_edit(Edit::insertion( - comment.to_string(), - TextSize::new(0), - ))), + .with_fix(Fix::safe_edit(Edit::insertion(comment, TextSize::new(0)))), ) } } @@ -169,10 +166,7 @@ impl TestRule for StableTestRuleUnsafeFix { StableTestRuleUnsafeFix, ruff_text_size::TextRange::default(), ) - .with_fix(Fix::unsafe_edit(Edit::insertion( - comment.to_string(), - TextSize::new(0), - ))), + .with_fix(Fix::unsafe_edit(Edit::insertion(comment, TextSize::new(0)))), ) } } @@ -217,7 +211,7 @@ impl TestRule for StableTestRuleDisplayOnlyFix { ruff_text_size::TextRange::default(), ) .with_fix(Fix::display_only_edit(Edit::insertion( - comment.to_string(), + comment, TextSize::new(0), ))), ) diff --git a/crates/ruff_macros/src/config.rs b/crates/ruff_macros/src/config.rs index 6300f08c7c..0df56a2256 100644 --- a/crates/ruff_macros/src/config.rs +++ b/crates/ruff_macros/src/config.rs @@ -60,15 +60,13 @@ pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result { } } - let docs: Vec<&Attribute> = struct_attributes + let docs = struct_attributes .iter() - .filter(|attr| attr.path().is_ident("doc")) - .collect(); + .filter(|attr| attr.path().is_ident("doc")); // Convert the list of `doc` attributes into a single string. let doc = dedent( &docs - .into_iter() .map(parse_doc) .collect::>>()? .join("\n"), diff --git a/crates/ruff_python_trivia/src/tokenizer.rs b/crates/ruff_python_trivia/src/tokenizer.rs index b480214acb..9b42665235 100644 --- a/crates/ruff_python_trivia/src/tokenizer.rs +++ b/crates/ruff_python_trivia/src/tokenizer.rs @@ -30,7 +30,10 @@ pub fn find_only_token_in_range( let token = tokens.next().expect("Expected a token"); debug_assert_eq!(token.kind(), token_kind); let mut tokens = tokens.skip_while(|token| token.kind == SimpleTokenKind::LParen); - debug_assert_eq!(tokens.next(), None); + #[allow(clippy::debug_assert_with_mut_call)] + { + debug_assert_eq!(tokens.next(), None); + } token } diff --git a/crates/ruff_server/tests/notebook.rs b/crates/ruff_server/tests/notebook.rs index 52a63b1919..d9e3f690b1 100644 --- a/crates/ruff_server/tests/notebook.rs +++ b/crates/ruff_server/tests/notebook.rs @@ -298,7 +298,7 @@ fn super_resolution_overview() { .unwrap(); } - let snapshot = session.take_snapshot(file_url.clone()).unwrap(); + let snapshot = session.take_snapshot(file_url).unwrap(); insta::assert_snapshot!( "changed_notebook", diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 17a1deb6c3..40aebcdc4f 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -2029,11 +2029,11 @@ mod tests { assert_override( vec![ RuleSelection { - select: Some(vec![d417.clone()]), + select: Some(vec![d417]), ..RuleSelection::default() }, RuleSelection { - extend_select: vec![d41.clone()], + extend_select: vec![d41], ..RuleSelection::default() }, ],