Skip to content

Commit

Permalink
Reduce explcit clones (#3793)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Mar 29, 2023
1 parent b6f1fed commit 595cd06
Show file tree
Hide file tree
Showing 73 changed files with 559 additions and 486 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/autofix/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub fn remove_unused_imports<'a>(
delete_stmt(stmt, parent, deleted, locator, indexer, stylist)
} else {
let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down
575 changes: 301 additions & 274 deletions crates/ruff/src/checkers/ast/mod.rs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/ruff/src/docstrings/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ pub struct Docstring<'a> {
pub indentation: &'a str,
}

#[derive(Copy, Clone)]
pub enum Documentable {
Class,
Function,
}

pub fn transition_scope(scope: &VisibleScope, stmt: &Stmt, kind: &Documentable) -> VisibleScope {
pub fn transition_scope(scope: VisibleScope, stmt: &Stmt, kind: Documentable) -> VisibleScope {
match kind {
Documentable::Function => VisibleScope {
modifier: Modifier::Function,
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/docstrings/extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub fn docstring_from(suite: &[Stmt]) -> Option<&Expr> {

/// Extract a `Definition` from the AST node defined by a `Stmt`.
pub fn extract<'a>(
scope: &VisibleScope,
scope: VisibleScope,
stmt: &'a Stmt,
body: &'a [Stmt],
kind: &Documentable,
kind: Documentable,
) -> Definition<'a> {
let expr = docstring_from(body);
match kind {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/docstrings/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub(crate) struct SectionContext<'a> {
pub(crate) original_index: usize,
}

fn suspected_as_section(line: &str, style: &SectionStyle) -> Option<SectionKind> {
fn suspected_as_section(line: &str, style: SectionStyle) -> Option<SectionKind> {
if let Some(kind) = SectionKind::from_str(whitespace::leading_words(line)) {
if style.sections().contains(&kind) {
return Some(kind);
Expand Down Expand Up @@ -168,7 +168,7 @@ fn is_docstring_section(context: &SectionContext) -> bool {
/// Extract all `SectionContext` values from a docstring.
pub(crate) fn section_contexts<'a>(
lines: &'a [&'a str],
style: &SectionStyle,
style: SectionStyle,
) -> Vec<SectionContext<'a>> {
let mut contexts = vec![];
for (kind, lineno) in lines
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/docstrings/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::docstrings::google::GOOGLE_SECTIONS;
use crate::docstrings::numpy::NUMPY_SECTIONS;
use crate::docstrings::sections::SectionKind;

#[derive(Copy, Clone)]
pub(crate) enum SectionStyle {
Numpy,
Google,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/flake8_to_ruff/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ pub fn convert(
if let Some(src_paths) = &isort.src_paths {
match options.src.as_mut() {
Some(src) => {
src.extend(src_paths.clone());
src.extend_from_slice(src_paths);
}
None => {
options.src = Some(src_paths.clone());
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/flake8_to_ruff/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct Token {
src: String,
}

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
enum TokenType {
Code,
File,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/flake8_to_ruff/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::anyhow;
use crate::registry::Linter;
use crate::rule_selector::RuleSelector;

#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum Plugin {
Flake82020,
Flake8Annotations,
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/jupyter/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub enum CodemirrorMode {
}

/// String identifying the type of cell.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Copy, Clone)]
pub enum CellType {
#[serde(rename = "code")]
Code,
Expand All @@ -236,14 +236,14 @@ pub enum CellType {
Raw,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
pub enum ScrolledEnum {
#[serde(rename = "auto")]
Auto,
}

/// Type of cell output.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
pub enum OutputType {
#[serde(rename = "display_data")]
DisplayData,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/lex/docstring_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use rustpython_parser::Tok;

#[derive(Default)]
#[derive(Default, Copy, Clone)]
enum State {
// Start of the module: first string gets marked as a docstring.
#[default]
Expand Down
20 changes: 10 additions & 10 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub fn add_noqa(
contents: &str,
commented_lines: &[usize],
noqa_line_for: &IntMap<usize, usize>,
line_ending: &LineEnding,
line_ending: LineEnding,
) -> Result<usize> {
let (count, output) = add_noqa_inner(
diagnostics,
Expand All @@ -206,7 +206,7 @@ fn add_noqa_inner(
contents: &str,
commented_lines: &[usize],
noqa_line_for: &IntMap<usize, usize>,
line_ending: &LineEnding,
line_ending: LineEnding,
) -> (usize, String) {
// Map of line number to set of (non-ignored) diagnostic codes that are triggered on that line.
let mut matches_by_line: FxHashMap<usize, RuleSet> = FxHashMap::default();
Expand Down Expand Up @@ -288,7 +288,7 @@ fn add_noqa_inner(
match matches_by_line.get(&lineno) {
None => {
output.push_str(line);
output.push_str(line_ending);
output.push_str(&line_ending);
}
Some(rules) => {
match extract_noqa_directive(line) {
Expand All @@ -301,13 +301,13 @@ fn add_noqa_inner(

// Add codes.
push_codes(&mut output, rules.iter().map(|rule| rule.noqa_code()));
output.push_str(line_ending);
output.push_str(&line_ending);
count += 1;
}
Directive::All(..) => {
// Leave the line as-is.
output.push_str(line);
output.push_str(line_ending);
output.push_str(&line_ending);
}
Directive::Codes(_, start_byte, _, existing, _) => {
// Reconstruct the line based on the preserved rule codes.
Expand All @@ -331,7 +331,7 @@ fn add_noqa_inner(
);

output.push_str(&formatted);
output.push_str(line_ending);
output.push_str(&line_ending);

// Only count if the new line is an actual edit.
if formatted != line {
Expand Down Expand Up @@ -395,7 +395,7 @@ mod tests {
contents,
&commented_lines,
&noqa_line_for,
&LineEnding::Lf,
LineEnding::Lf,
);
assert_eq!(count, 0);
assert_eq!(output, format!("{contents}\n"));
Expand All @@ -414,7 +414,7 @@ mod tests {
contents,
&commented_lines,
&noqa_line_for,
&LineEnding::Lf,
LineEnding::Lf,
);
assert_eq!(count, 1);
assert_eq!(output, "x = 1 # noqa: F841\n");
Expand All @@ -439,7 +439,7 @@ mod tests {
contents,
&commented_lines,
&noqa_line_for,
&LineEnding::Lf,
LineEnding::Lf,
);
assert_eq!(count, 1);
assert_eq!(output, "x = 1 # noqa: E741, F841\n");
Expand All @@ -464,7 +464,7 @@ mod tests {
contents,
&commented_lines,
&noqa_line_for,
&LineEnding::Lf,
LineEnding::Lf,
);
assert_eq!(count, 0);
assert_eq!(output, "x = 1 # noqa\n");
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl PyprojectDiscovery {
}

/// The strategy for resolving file paths in a `pyproject.toml`.
#[derive(Copy, Clone)]
pub enum Relativity {
/// Resolve file paths relative to the current working directory.
Cwd,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rule_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl RuleSelector {
}
}

#[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord)]
#[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub(crate) enum Specificity {
All,
LinterGroup,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_annotations/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ fn check_dynamically_typed<F>(
pub fn definition(
checker: &Checker,
definition: &Definition,
visibility: &Visibility,
visibility: Visibility,
) -> Vec<Diagnostic> {
// TODO(charlie): Consider using the AST directly here rather than `Definition`.
// We could adhere more closely to `flake8-annotations` by defining public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ruff_python_ast::types::Range;

use crate::checkers::ast::Checker;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Kind {
Expression,
Attribute,
Expand Down
30 changes: 15 additions & 15 deletions crates/ruff/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn fix_unnecessary_generator_list(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn fix_unnecessary_generator_set(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn fix_unnecessary_generator_dict(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -237,7 +237,7 @@ pub fn fix_unnecessary_list_comprehension_set(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -293,7 +293,7 @@ pub fn fix_unnecessary_list_comprehension_dict(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -387,7 +387,7 @@ pub fn fix_unnecessary_literal_set(
}

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -460,7 +460,7 @@ pub fn fix_unnecessary_literal_dict(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -576,7 +576,7 @@ pub fn fix_unnecessary_collection_call(
};

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -635,7 +635,7 @@ pub fn fix_unnecessary_literal_within_tuple_call(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -696,7 +696,7 @@ pub fn fix_unnecessary_literal_within_list_call(
}));

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -725,7 +725,7 @@ pub fn fix_unnecessary_list_call(
body.value = arg.value.clone();

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -854,7 +854,7 @@ pub fn fix_unnecessary_call_around_sorted(
}

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -893,7 +893,7 @@ pub fn fix_unnecessary_double_cast_or_process(
outer_call.args = inner_call.clone();

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -989,7 +989,7 @@ pub fn fix_unnecessary_comprehension(
}

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down Expand Up @@ -1142,7 +1142,7 @@ pub fn fix_unnecessary_map(
}

let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_newline: &stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_debugger/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum DebuggerUsingType {
Call(String),
Import(String),
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/flake8_logging_format/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ fn check_log_record_attr_clash(checker: &mut Checker, extra: &Keyword) {
}
}

#[derive(Copy, Clone)]
enum LoggingCallType {
/// Logging call with a level method, e.g., `logging.info`.
LevelCall(LoggingLevel),
Expand Down

0 comments on commit 595cd06

Please sign in to comment.