From be4f0a1582531d2d0540f2f5beae0e8eec81d19a Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Wed, 18 Sep 2024 17:47:45 +0200 Subject: [PATCH] refactor: allow new clippy rules --- Cargo.toml | 27 ++++++++++------ crates/biome_grit_patterns/src/grit_node.rs | 2 +- .../src/grit_target_node.rs | 2 +- .../no_excessive_cognitive_complexity.rs | 5 +-- .../nursery/use_consistent_curly_braces.rs | 19 +++++------- .../biome_js_analyze/src/services/manifest.rs | 12 +++---- .../src/suppression_action.rs | 22 ++++++------- crates/biome_js_syntax/src/expr_ext.rs | 3 +- .../src/file_handlers/javascript.rs | 16 +++++----- crates/biome_service/src/file_handlers/mod.rs | 3 +- crates/biome_service/src/workspace.rs | 3 +- crates/biome_service/src/workspace/server.rs | 31 ++++++++----------- 12 files changed, 65 insertions(+), 80 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dc49c74bb6b9..7e5b100c055a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ empty_docs = "allow" # there are some false positives inside biome_ multiple_crate_versions = "allow" # pedantic +assigning_clones = "warn" checked_conversions = "warn" cloned_instead_of_copied = "warn" copy_iterator = "warn" @@ -35,10 +36,12 @@ implicit_hasher = "warn" index_refutable_slice = "warn" inefficient_to_string = "warn" invalid_upcast_comparisons = "warn" +iter_filter_is_ok = "warn" iter_not_returning_iterator = "warn" large_stack_arrays = "warn" large_types_passed_by_value = "warn" macro_use_imports = "warn" +manual_is_variant_and = "warn" manual_ok_or = "warn" manual_string_new = "warn" map_flatten = "warn" @@ -50,9 +53,12 @@ needless_bitwise_bool = "warn" needless_continue = "warn" needless_for_each = "warn" no_effect_underscore_binding = "warn" +option_as_ref_cloned = "warn" ref_binding_to_reference = "warn" ref_option_ref = "warn" +single_char_pattern = "warn" stable_sort_primitive = "warn" +str_split_at_newline = "warn" unnecessary_box_returns = "warn" unnecessary_join = "warn" unnested_or_patterns = "warn" @@ -61,15 +67,18 @@ verbose_bit_mask = "warn" zero_sized_map_values = "warn" # restriction -empty_drop = "warn" -float_cmp_const = "warn" -get_unwrap = "warn" -infinite_loop = "warn" -lossy_float_literal = "warn" -rc_buffer = "warn" -rc_mutex = "warn" -rest_pat_in_fully_bound_structs = "warn" -verbose_file_reads = "warn" +cfg_not_test = "warn" +empty_drop = "warn" +empty_enum_variants_with_brackets = "warn" +float_cmp_const = "warn" +get_unwrap = "warn" +infinite_loop = "warn" +lossy_float_literal = "warn" +rc_buffer = "warn" +rc_mutex = "warn" +rest_pat_in_fully_bound_structs = "warn" +string_lit_chars_any = "warn" +verbose_file_reads = "warn" # https://github.com/rustwasm/wasm-bindgen/issues/3944 #mem_forget = "warn" diff --git a/crates/biome_grit_patterns/src/grit_node.rs b/crates/biome_grit_patterns/src/grit_node.rs index 944568647947..b08170d5af1e 100644 --- a/crates/biome_grit_patterns/src/grit_node.rs +++ b/crates/biome_grit_patterns/src/grit_node.rs @@ -116,7 +116,7 @@ impl Iterator for AncestorIterator { type Item = GritNode; fn next(&mut self) -> Option { - let node = self.node.as_ref().cloned()?; + let node = self.node.clone()?; self.node = node.parent(); Some(node) } diff --git a/crates/biome_grit_patterns/src/grit_target_node.rs b/crates/biome_grit_patterns/src/grit_target_node.rs index 6af59685ae2e..eefdd9a28a24 100644 --- a/crates/biome_grit_patterns/src/grit_target_node.rs +++ b/crates/biome_grit_patterns/src/grit_target_node.rs @@ -386,7 +386,7 @@ impl<'a> Iterator for AncestorIterator<'a> { type Item = GritTargetNode<'a>; fn next(&mut self) -> Option { - let node = self.node.as_ref().cloned()?; + let node = self.node.clone()?; self.node = node.parent(); Some(node) } diff --git a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs index 97f2bc393e98..e7f0390a094a 100644 --- a/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs +++ b/crates/biome_js_analyze/src/lint/complexity/no_excessive_cognitive_complexity.rs @@ -198,10 +198,7 @@ impl Visitor for CognitiveComplexityVisitor { impl CognitiveComplexityVisitor { fn on_enter(&mut self, node: &JsSyntaxNode) { let parent = self.stack.last(); - if parent - .map(|parent| parent.score == MAX_SCORE) - .unwrap_or_default() - { + if parent.is_some_and(|parent| parent.score == MAX_SCORE) { return; // No need for further processing if we're already at the max. } diff --git a/crates/biome_js_analyze/src/lint/nursery/use_consistent_curly_braces.rs b/crates/biome_js_analyze/src/lint/nursery/use_consistent_curly_braces.rs index 2587ca2d4c6f..a6f5c1a9156c 100644 --- a/crates/biome_js_analyze/src/lint/nursery/use_consistent_curly_braces.rs +++ b/crates/biome_js_analyze/src/lint/nursery/use_consistent_curly_braces.rs @@ -386,20 +386,17 @@ fn has_curly_braces(node: &AnyJsxCurlyQuery) -> bool { } fn contains_string_literal(node: &JsxExpressionAttributeValue) -> bool { - node.expression() - .map(|expr| { - matches!( - expr, - AnyJsExpression::AnyJsLiteralExpression( - AnyJsLiteralExpression::JsStringLiteralExpression(_) - ) + node.expression().is_ok_and(|expr| { + matches!( + expr, + AnyJsExpression::AnyJsLiteralExpression( + AnyJsLiteralExpression::JsStringLiteralExpression(_) ) - }) - .unwrap_or_default() + ) + }) } fn contains_jsx_tag(node: &JsxExpressionAttributeValue) -> bool { node.expression() - .map(|expr| matches!(expr, AnyJsExpression::JsxTagExpression(_))) - .unwrap_or_default() + .is_ok_and(|expr| matches!(expr, AnyJsExpression::JsxTagExpression(_))) } diff --git a/crates/biome_js_analyze/src/services/manifest.rs b/crates/biome_js_analyze/src/services/manifest.rs index 6ffd45c726ed..c471d8da1064 100644 --- a/crates/biome_js_analyze/src/services/manifest.rs +++ b/crates/biome_js_analyze/src/services/manifest.rs @@ -24,32 +24,28 @@ impl ManifestServices { self.manifest .as_ref() .as_ref() - .map(|pkg| pkg.dependencies.contains(specifier)) - .unwrap_or_default() + .is_some_and(|pkg| pkg.dependencies.contains(specifier)) } pub(crate) fn is_dev_dependency(&self, specifier: &str) -> bool { self.manifest .as_ref() .as_ref() - .map(|pkg| pkg.dev_dependencies.contains(specifier)) - .unwrap_or_default() + .is_some_and(|pkg| pkg.dev_dependencies.contains(specifier)) } pub(crate) fn is_peer_dependency(&self, specifier: &str) -> bool { self.manifest .as_ref() .as_ref() - .map(|pkg| pkg.peer_dependencies.contains(specifier)) - .unwrap_or_default() + .is_some_and(|pkg| pkg.peer_dependencies.contains(specifier)) } pub(crate) fn is_optional_dependency(&self, specifier: &str) -> bool { self.manifest .as_ref() .as_ref() - .map(|pkg| pkg.optional_dependencies.contains(specifier)) - .unwrap_or_default() + .is_some_and(|pkg| pkg.optional_dependencies.contains(specifier)) } } diff --git a/crates/biome_js_analyze/src/suppression_action.rs b/crates/biome_js_analyze/src/suppression_action.rs index d2d0b8e2571e..b86f20ee5532 100644 --- a/crates/biome_js_analyze/src/suppression_action.rs +++ b/crates/biome_js_analyze/src/suppression_action.rs @@ -65,17 +65,14 @@ impl SuppressionAction for JsSuppressionAction { // There are some tokens that might contains newlines in their tokens, only // few nodes matches this criteria. If the token is inside one of those nodes, // then we check its content. - let nodes_that_might_contain_newlines = current_token - .parent() - .map(|node| { - matches!( - node.kind(), - JsSyntaxKind::JSX_TEXT - | JsSyntaxKind::JS_STRING_LITERAL - | JsSyntaxKind::TEMPLATE_CHUNK - ) - }) - .unwrap_or_default(); + let nodes_that_might_contain_newlines = current_token.parent().is_some_and(|node| { + matches!( + node.kind(), + JsSyntaxKind::JSX_TEXT + | JsSyntaxKind::JS_STRING_LITERAL + | JsSyntaxKind::TEMPLATE_CHUNK + ) + }); if current_token .trailing_trivia() .pieces() @@ -154,8 +151,7 @@ impl SuppressionAction for JsSuppressionAction { // quick check is the element is inside a list if current_jsx_element .parent() - .map(|p| JsxChildList::can_cast(p.kind())) - .unwrap_or_default() + .is_some_and(|p| JsxChildList::can_cast(p.kind())) { let jsx_comment = jsx_expression_child( token(T!['{']).with_trailing_trivia([( diff --git a/crates/biome_js_syntax/src/expr_ext.rs b/crates/biome_js_syntax/src/expr_ext.rs index 7654cfcdea7e..995a533e61cd 100644 --- a/crates/biome_js_syntax/src/expr_ext.rs +++ b/crates/biome_js_syntax/src/expr_ext.rs @@ -85,8 +85,7 @@ impl JsReferenceIdentifier { /// ``` pub fn has_name(&self, name: &str) -> bool { self.value_token() - .map(|token| token.text_trimmed() == name) - .unwrap_or_default() + .is_ok_and(|token| token.text_trimmed() == name) } pub fn name(&self) -> SyntaxResult { diff --git a/crates/biome_service/src/file_handlers/javascript.rs b/crates/biome_service/src/file_handlers/javascript.rs index d093b063d301..b5d694b3194c 100644 --- a/crates/biome_service/src/file_handlers/javascript.rs +++ b/crates/biome_service/src/file_handlers/javascript.rs @@ -323,15 +323,13 @@ fn parse( ) -> ParseResult { let mut options = JsParserOptions { grit_metavariables: false, - parse_class_parameter_decorators: settings - .map(|settings| { - settings - .languages - .javascript - .parser - .parse_class_parameter_decorators - }) - .unwrap_or_default(), + parse_class_parameter_decorators: settings.is_some_and(|settings| { + settings + .languages + .javascript + .parser + .parse_class_parameter_decorators + }), }; if let Some(settings) = settings { options = settings diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 2da2abb62147..76ae2cb2e158 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -1006,8 +1006,7 @@ impl<'a, 'b> AssistsVisitor<'a, 'b> { let organize_imports_enabled = self .settings - .map(|settings| settings.organize_imports.enabled) - .unwrap_or_default(); + .is_some_and(|settings| settings.organize_imports.enabled); if organize_imports_enabled && self.import_sorting.match_rule::() { self.enabled_rules.push(self.import_sorting); return; diff --git a/crates/biome_service/src/workspace.rs b/crates/biome_service/src/workspace.rs index 5416b5faad7f..2b6fcdc45036 100644 --- a/crates/biome_service/src/workspace.rs +++ b/crates/biome_service/src/workspace.rs @@ -255,8 +255,7 @@ impl FileFeaturesResult { fn supports_for(&self, feature: &FeatureKind) -> bool { self.features_supported .get(feature) - .map(|support_kind| matches!(support_kind, SupportKind::Supported)) - .unwrap_or_default() + .is_some_and(|support_kind| matches!(support_kind, SupportKind::Supported)) } pub fn supports_lint(&self) -> bool { diff --git a/crates/biome_service/src/workspace/server.rs b/crates/biome_service/src/workspace/server.rs index b3f70d5fa443..c8aa6ae6412d 100644 --- a/crates/biome_service/src/workspace/server.rs +++ b/crates/biome_service/src/workspace/server.rs @@ -311,24 +311,19 @@ impl WorkspaceServer { || settings.files.included_files.matches_path(path); !is_included || settings.files.ignored_files.matches_path(path) - || settings - .files - .git_ignore - .as_ref() - .map(|ignore| { - // `matched_path_or_any_parents` panics if `source` is not under the gitignore root. - // This checks excludes absolute paths that are not a prefix of the base root. - if !path.has_root() || path.starts_with(ignore.path()) { - // Because Biome passes a list of paths, - // we use `matched_path_or_any_parents` instead of `matched`. - ignore - .matched_path_or_any_parents(path, path.is_dir()) - .is_ignore() - } else { - false - } - }) - .unwrap_or_default() + || settings.files.git_ignore.as_ref().is_some_and(|ignore| { + // `matched_path_or_any_parents` panics if `source` is not under the gitignore root. + // This checks excludes absolute paths that are not a prefix of the base root. + if !path.has_root() || path.starts_with(ignore.path()) { + // Because Biome passes a list of paths, + // we use `matched_path_or_any_parents` instead of `matched`. + ignore + .matched_path_or_any_parents(path, path.is_dir()) + .is_ignore() + } else { + false + } + }) } /// Check whether a file is ignored in the feature `ignore`/`include`