mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00
Reduce explcit clones (#3793)
This commit is contained in:
parent
b6f1fed424
commit
595cd065f3
73 changed files with 557 additions and 484 deletions
|
@ -65,7 +65,7 @@ pub struct Generator<'a> {
|
|||
/// The quote style to use for string literals.
|
||||
quote: Quote,
|
||||
/// The line ending to use.
|
||||
line_ending: &'a LineEnding,
|
||||
line_ending: LineEnding,
|
||||
buffer: String,
|
||||
indent_depth: usize,
|
||||
num_newlines: usize,
|
||||
|
@ -87,7 +87,7 @@ impl<'a> From<&'a Stylist<'a>> for Generator<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Generator<'a> {
|
||||
pub const fn new(indent: &'a Indentation, quote: Quote, line_ending: &'a LineEnding) -> Self {
|
||||
pub const fn new(indent: &'a Indentation, quote: Quote, line_ending: LineEnding) -> Self {
|
||||
Self {
|
||||
// Style preferences.
|
||||
indent,
|
||||
|
@ -128,7 +128,7 @@ impl<'a> Generator<'a> {
|
|||
fn p(&mut self, s: &str) {
|
||||
if self.num_newlines > 0 {
|
||||
for _ in 0..self.num_newlines {
|
||||
self.buffer += self.line_ending;
|
||||
self.buffer += &self.line_ending;
|
||||
}
|
||||
self.num_newlines = 0;
|
||||
}
|
||||
|
@ -1266,7 +1266,7 @@ mod tests {
|
|||
let line_ending = LineEnding::default();
|
||||
let program = parser::parse_program(contents, "<filename>").unwrap();
|
||||
let stmt = program.first().unwrap();
|
||||
let mut generator = Generator::new(&indentation, quote, &line_ending);
|
||||
let mut generator = Generator::new(&indentation, quote, line_ending);
|
||||
generator.unparse_stmt(stmt);
|
||||
generator.generate()
|
||||
}
|
||||
|
@ -1274,7 +1274,7 @@ mod tests {
|
|||
fn round_trip_with(
|
||||
indentation: &Indentation,
|
||||
quote: Quote,
|
||||
line_ending: &LineEnding,
|
||||
line_ending: LineEnding,
|
||||
contents: &str,
|
||||
) -> String {
|
||||
let program = parser::parse_program(contents, "<filename>").unwrap();
|
||||
|
@ -1449,7 +1449,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::Double,
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#""hello""#
|
||||
),
|
||||
r#""hello""#
|
||||
|
@ -1458,7 +1458,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::Single,
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#""hello""#
|
||||
),
|
||||
r#"'hello'"#
|
||||
|
@ -1467,7 +1467,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::Double,
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#"'hello'"#
|
||||
),
|
||||
r#""hello""#
|
||||
|
@ -1476,7 +1476,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::Single,
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#"'hello'"#
|
||||
),
|
||||
r#"'hello'"#
|
||||
|
@ -1489,7 +1489,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::new(" ".to_string()),
|
||||
Quote::default(),
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#"
|
||||
if True:
|
||||
pass
|
||||
|
@ -1507,7 +1507,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::new(" ".to_string()),
|
||||
Quote::default(),
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#"
|
||||
if True:
|
||||
pass
|
||||
|
@ -1525,7 +1525,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::new("\t".to_string()),
|
||||
Quote::default(),
|
||||
&LineEnding::default(),
|
||||
LineEnding::default(),
|
||||
r#"
|
||||
if True:
|
||||
pass
|
||||
|
@ -1547,7 +1547,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::default(),
|
||||
&LineEnding::Lf,
|
||||
LineEnding::Lf,
|
||||
"if True:\n print(42)",
|
||||
),
|
||||
"if True:\n print(42)",
|
||||
|
@ -1557,7 +1557,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::default(),
|
||||
&LineEnding::CrLf,
|
||||
LineEnding::CrLf,
|
||||
"if True:\n print(42)",
|
||||
),
|
||||
"if True:\r\n print(42)",
|
||||
|
@ -1567,7 +1567,7 @@ if True:
|
|||
round_trip_with(
|
||||
&Indentation::default(),
|
||||
Quote::default(),
|
||||
&LineEnding::Cr,
|
||||
LineEnding::Cr,
|
||||
"if True:\n print(42)",
|
||||
),
|
||||
"if True:\r print(42)",
|
||||
|
|
|
@ -56,8 +56,9 @@ impl<'a> Stylist<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn line_ending(&'a self) -> &'a LineEnding {
|
||||
self.line_ending
|
||||
pub fn line_ending(&'a self) -> LineEnding {
|
||||
*self
|
||||
.line_ending
|
||||
.get_or_init(|| detect_line_ending(self.locator.contents()).unwrap_or_default())
|
||||
}
|
||||
|
||||
|
@ -160,7 +161,7 @@ impl Deref for Indentation {
|
|||
|
||||
/// The line ending style used in Python source code.
|
||||
/// See <https://docs.python.org/3/reference/lexical_analysis.html#physical-lines>
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum LineEnding {
|
||||
Lf,
|
||||
Cr,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue