From 2464cf6fe9940ffe888baf8cc115918e1223104d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 5 Jan 2023 18:56:03 -0500 Subject: [PATCH] Fix some `&String`, `&Option`, and `&Vec` usages (#1670) --- src/ast/helpers.rs | 2 +- src/checkers/ast.rs | 30 +++++++------ .../checks/hardcoded_password_string.rs | 2 +- src/flake8_pytest_style/plugins/imports.rs | 4 +- src/flake8_pytest_style/plugins/raises.rs | 6 +-- src/flake8_return/visitor.rs | 2 +- src/isort/mod.rs | 43 +++++++++++++------ src/isort/types.rs | 4 +- src/printer.rs | 2 +- src/pyflakes/checks.rs | 2 +- .../plugins/rewrite_unicode_literal.rs | 2 +- .../plugins/unnecessary_encode_utf8.rs | 6 +-- .../plugins/unnecessary_future_import.rs | 2 +- 13 files changed, 65 insertions(+), 42 deletions(-) diff --git a/src/ast/helpers.rs b/src/ast/helpers.rs index 0c16f4bccadc4..a76783f3b4f1a 100644 --- a/src/ast/helpers.rs +++ b/src/ast/helpers.rs @@ -316,7 +316,7 @@ pub fn is_super_call_with_arguments(func: &Expr, args: &[Expr]) -> bool { } /// Format the module name for a relative import. -pub fn format_import_from(level: Option<&usize>, module: Option<&String>) -> String { +pub fn format_import_from(level: Option<&usize>, module: Option<&str>) -> String { let mut module_name = String::with_capacity(16); if let Some(level) = level { for _ in 0..*level { diff --git a/src/checkers/ast.rs b/src/checkers/ast.rs index 86f88b9d4e2f5..ccb171f527449 100644 --- a/src/checkers/ast.rs +++ b/src/checkers/ast.rs @@ -926,9 +926,11 @@ where } if self.settings.enabled.contains(&CheckCode::PT013) { - if let Some(check) = - flake8_pytest_style::plugins::import_from(stmt, module, level) - { + if let Some(check) = flake8_pytest_style::plugins::import_from( + stmt, + module.as_ref().map(String::as_str), + level.as_ref(), + ) { self.add_check(check); } } @@ -992,7 +994,7 @@ where self.add_check(Check::new( CheckKind::ImportStarNotPermitted(helpers::format_import_from( level.as_ref(), - module.as_ref(), + module.as_ref().map(String::as_str), )), Range::from_located(stmt), )); @@ -1003,7 +1005,7 @@ where self.add_check(Check::new( CheckKind::ImportStarUsed(helpers::format_import_from( level.as_ref(), - module.as_ref(), + module.as_ref().map(String::as_str), )), Range::from_located(stmt), )); @@ -2577,7 +2579,11 @@ where } } if self.settings.enabled.contains(&CheckCode::UP025) { - pyupgrade::plugins::rewrite_unicode_literal(self, expr, kind); + pyupgrade::plugins::rewrite_unicode_literal( + self, + expr, + kind.as_ref().map(String::as_str), + ); } } ExprKind::Lambda { args, body, .. } => { @@ -3376,7 +3382,7 @@ impl<'a> Checker<'a> { if let BindingKind::StarImportation(level, module) = &binding.kind { from_list.push(helpers::format_import_from( level.as_ref(), - module.as_ref(), + module.as_ref().map(String::as_str), )); } } @@ -3857,7 +3863,7 @@ impl<'a> Checker<'a> { if let BindingKind::StarImportation(level, module) = &binding.kind { from_list.push(helpers::format_import_from( level.as_ref(), - module.as_ref(), + module.as_ref().map(String::as_str), )); } } @@ -3882,7 +3888,7 @@ impl<'a> Checker<'a> { if self.settings.enabled.contains(&CheckCode::F401) { // Collect all unused imports by location. (Multiple unused imports at the same // location indicates an `import from`.) - type UnusedImport<'a> = (&'a String, &'a Range); + type UnusedImport<'a> = (&'a str, &'a Range); type BindingContext<'a, 'b> = (&'a RefEquality<'b, Stmt>, Option<&'a RefEquality<'b, Stmt>>); @@ -3954,9 +3960,7 @@ impl<'a> Checker<'a> { let deleted: Vec<&Stmt> = self.deletions.iter().map(|node| node.0).collect(); match autofix::helpers::remove_unused_imports( - unused_imports - .iter() - .map(|(full_name, _)| full_name.as_str()), + unused_imports.iter().map(|(full_name, _)| *full_name), child, parent, &deleted, @@ -4002,7 +4006,7 @@ impl<'a> Checker<'a> { let multiple = unused_imports.len() > 1; for (full_name, range) in unused_imports { let mut check = Check::new( - CheckKind::UnusedImport(full_name.clone(), ignore_init, multiple), + CheckKind::UnusedImport(full_name.to_string(), ignore_init, multiple), *range, ); if matches!(child.node, StmtKind::ImportFrom { .. }) diff --git a/src/flake8_bandit/checks/hardcoded_password_string.rs b/src/flake8_bandit/checks/hardcoded_password_string.rs index af5608c6822ec..8b31e71a8e4cd 100644 --- a/src/flake8_bandit/checks/hardcoded_password_string.rs +++ b/src/flake8_bandit/checks/hardcoded_password_string.rs @@ -42,7 +42,7 @@ pub fn compare_to_hardcoded_password_string(left: &Expr, comparators: &[Expr]) - } /// S105 -pub fn assign_hardcoded_password_string(value: &Expr, targets: &Vec) -> Option { +pub fn assign_hardcoded_password_string(value: &Expr, targets: &[Expr]) -> Option { if let Some(string) = string_literal(value) { for target in targets { if is_password_target(target) { diff --git a/src/flake8_pytest_style/plugins/imports.rs b/src/flake8_pytest_style/plugins/imports.rs index f43a0aebc158d..876518181921a 100644 --- a/src/flake8_pytest_style/plugins/imports.rs +++ b/src/flake8_pytest_style/plugins/imports.rs @@ -25,8 +25,8 @@ pub fn import(import_from: &Stmt, name: &str, asname: Option<&str>) -> Option, - level: &Option, + module: Option<&str>, + level: Option<&usize>, ) -> Option { // If level is not zero or module is none, return if let Some(level) = level { diff --git a/src/flake8_pytest_style/plugins/raises.rs b/src/flake8_pytest_style/plugins/raises.rs index 67387ece3c28b..702ea63f20605 100644 --- a/src/flake8_pytest_style/plugins/raises.rs +++ b/src/flake8_pytest_style/plugins/raises.rs @@ -18,7 +18,7 @@ fn is_pytest_raises( match_module_member(func, "pytest", "raises", from_imports, import_aliases) } -fn is_non_trivial_with_body(body: &Vec) -> bool { +fn is_non_trivial_with_body(body: &[Stmt]) -> bool { if body.len() > 1 { true } else if let Some(first_body_stmt) = body.first() { @@ -28,7 +28,7 @@ fn is_non_trivial_with_body(body: &Vec) -> bool { } } -pub fn raises_call(checker: &mut Checker, func: &Expr, args: &Vec, keywords: &Vec) { +pub fn raises_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: &[Keyword]) { if is_pytest_raises(func, &checker.from_imports, &checker.import_aliases) { if checker.settings.enabled.contains(&CheckCode::PT010) { if args.is_empty() && keywords.is_empty() { @@ -57,7 +57,7 @@ pub fn raises_call(checker: &mut Checker, func: &Expr, args: &Vec, keyword } } -pub fn complex_raises(checker: &mut Checker, stmt: &Stmt, items: &[Withitem], body: &Vec) { +pub fn complex_raises(checker: &mut Checker, stmt: &Stmt, items: &[Withitem], body: &[Stmt]) { let mut is_too_complex = false; let raises_called = items.iter().any(|item| match &item.context_expr.node { diff --git a/src/flake8_return/visitor.rs b/src/flake8_return/visitor.rs index c8a95a09dcf9d..10a05f59ffdac 100644 --- a/src/flake8_return/visitor.rs +++ b/src/flake8_return/visitor.rs @@ -62,7 +62,7 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> { StmtKind::Global { names } | StmtKind::Nonlocal { names } => { self.stack .non_locals - .extend(names.iter().map(std::string::String::as_str)); + .extend(names.iter().map(String::as_str)); } StmtKind::FunctionDef { .. } | StmtKind::AsyncFunctionDef { .. } => { // Don't recurse. diff --git a/src/isort/mod.rs b/src/isort/mod.rs index 91bf23b8af82f..f8425f3d5c230 100644 --- a/src/isort/mod.rs +++ b/src/isort/mod.rs @@ -33,7 +33,7 @@ pub mod types; #[derive(Debug)] pub struct AnnotatedAliasData<'a> { pub name: &'a str, - pub asname: Option<&'a String>, + pub asname: Option<&'a str>, pub atop: Vec>, pub inline: Vec>, } @@ -87,7 +87,7 @@ fn annotate_imports<'a>( .iter() .map(|alias| AliasData { name: &alias.node.name, - asname: alias.node.asname.as_ref(), + asname: alias.node.asname.as_deref(), }) .collect(), atop, @@ -145,7 +145,7 @@ fn annotate_imports<'a>( aliases.push(AnnotatedAliasData { name: &alias.node.name, - asname: alias.node.asname.as_ref(), + asname: alias.node.asname.as_deref(), atop: alias_atop, inline: alias_inline, }); @@ -219,19 +219,28 @@ fn normalize_imports(imports: Vec, combine_as_imports: bool) -> let entry = if alias.name == "*" { block .import_from_star - .entry(ImportFromData { module, level }) + .entry(ImportFromData { + module: module.map(String::as_str), + level, + }) .or_default() } else if alias.asname.is_none() || combine_as_imports { &mut block .import_from - .entry(ImportFromData { module, level }) + .entry(ImportFromData { + module: module.map(String::as_str), + level, + }) .or_default() .0 } else { block .import_from_as .entry(( - ImportFromData { module, level }, + ImportFromData { + module: module.map(String::as_str), + level, + }, AliasData { name: alias.name, asname: alias.asname, @@ -254,12 +263,18 @@ fn normalize_imports(imports: Vec, combine_as_imports: bool) -> let entry = if alias.name == "*" { block .import_from_star - .entry(ImportFromData { module, level }) + .entry(ImportFromData { + module: module.map(String::as_str), + level, + }) .or_default() } else if alias.asname.is_none() || combine_as_imports { block .import_from - .entry(ImportFromData { module, level }) + .entry(ImportFromData { + module: module.map(String::as_str), + level, + }) .or_default() .1 .entry(AliasData { @@ -271,7 +286,10 @@ fn normalize_imports(imports: Vec, combine_as_imports: bool) -> block .import_from_as .entry(( - ImportFromData { module, level }, + ImportFromData { + module: module.map(String::as_str), + level, + }, AliasData { name: alias.name, asname: alias.asname, @@ -290,9 +308,10 @@ fn normalize_imports(imports: Vec, combine_as_imports: bool) -> // Propagate trailing commas. if matches!(trailing_comma, TrailingComma::Present) { - if let Some(entry) = - block.import_from.get_mut(&ImportFromData { module, level }) - { + if let Some(entry) = block.import_from.get_mut(&ImportFromData { + module: module.map(String::as_str), + level, + }) { entry.2 = trailing_comma; } } diff --git a/src/isort/types.rs b/src/isort/types.rs index 5700a025b2ead..a16a26793941b 100644 --- a/src/isort/types.rs +++ b/src/isort/types.rs @@ -18,14 +18,14 @@ impl Default for TrailingComma { #[derive(Debug, Hash, Ord, PartialOrd, Eq, PartialEq, Clone)] pub struct ImportFromData<'a> { - pub module: Option<&'a String>, + pub module: Option<&'a str>, pub level: Option<&'a usize>, } #[derive(Debug, Hash, Ord, PartialOrd, Eq, PartialEq)] pub struct AliasData<'a> { pub name: &'a str, - pub asname: Option<&'a String>, + pub asname: Option<&'a str>, } #[derive(Debug, Default, Clone)] diff --git a/src/printer.rs b/src/printer.rs index 84a84f370726b..41254ebd6c146 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -303,7 +303,7 @@ impl<'a> Printer<'a> { } } -fn group_messages_by_filename(messages: &Vec) -> BTreeMap<&String, Vec<&Message>> { +fn group_messages_by_filename(messages: &[Message]) -> BTreeMap<&String, Vec<&Message>> { let mut grouped_messages = BTreeMap::default(); for message in messages { grouped_messages diff --git a/src/pyflakes/checks.rs b/src/pyflakes/checks.rs index d5e424f1700ea..817b8593ac437 100644 --- a/src/pyflakes/checks.rs +++ b/src/pyflakes/checks.rs @@ -132,7 +132,7 @@ pub fn default_except_not_last( #[derive(Debug, PartialEq)] enum DictionaryKey<'a> { Constant(&'a Constant), - Variable(&'a String), + Variable(&'a str), } fn convert_to_value(expr: &Expr) -> Option { diff --git a/src/pyupgrade/plugins/rewrite_unicode_literal.rs b/src/pyupgrade/plugins/rewrite_unicode_literal.rs index 3a02f683becee..dfcc7f2a6434c 100644 --- a/src/pyupgrade/plugins/rewrite_unicode_literal.rs +++ b/src/pyupgrade/plugins/rewrite_unicode_literal.rs @@ -6,7 +6,7 @@ use crate::checkers::ast::Checker; use crate::registry::{Check, CheckKind}; /// UP025 -pub fn rewrite_unicode_literal(checker: &mut Checker, expr: &Expr, kind: &Option) { +pub fn rewrite_unicode_literal(checker: &mut Checker, expr: &Expr, kind: Option<&str>) { if let Some(const_kind) = kind { if const_kind.to_lowercase() == "u" { let mut check = Check::new(CheckKind::RewriteUnicodeLiteral, Range::from_located(expr)); diff --git a/src/pyupgrade/plugins/unnecessary_encode_utf8.rs b/src/pyupgrade/plugins/unnecessary_encode_utf8.rs index 1897c3124b1b4..93ea496416c1f 100644 --- a/src/pyupgrade/plugins/unnecessary_encode_utf8.rs +++ b/src/pyupgrade/plugins/unnecessary_encode_utf8.rs @@ -34,7 +34,7 @@ fn is_utf8_encoding_arg(arg: &Expr) -> bool { } } -fn is_default_encode(args: &Vec, kwargs: &Vec) -> bool { +fn is_default_encode(args: &[Expr], kwargs: &[Keyword]) -> bool { match (args.len(), kwargs.len()) { // .encode() (0, 0) => true, @@ -106,8 +106,8 @@ pub fn unnecessary_encode_utf8( checker: &mut Checker, expr: &Expr, func: &Expr, - args: &Vec, - kwargs: &Vec, + args: &[Expr], + kwargs: &[Keyword], ) { let Some(variable) = match_encoded_variable(func) else { return; diff --git a/src/pyupgrade/plugins/unnecessary_future_import.rs b/src/pyupgrade/plugins/unnecessary_future_import.rs index 980aab681af82..a1244d0d28558 100644 --- a/src/pyupgrade/plugins/unnecessary_future_import.rs +++ b/src/pyupgrade/plugins/unnecessary_future_import.rs @@ -73,7 +73,7 @@ pub fn unnecessary_future_import(checker: &mut Checker, stmt: &Stmt, names: &[Lo .map(|alias| format!("__future__.{}", alias.node.name)) .collect(); match autofix::helpers::remove_unused_imports( - unused_imports.iter().map(std::string::String::as_str), + unused_imports.iter().map(String::as_str), defined_by.0, defined_in.map(|node| node.0), &deleted,