Skip to content

Commit b6d155d

Browse files
committed
Make Location Copy and remove all location.clone()s
1 parent 75eabfd commit b6d155d

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

compiler/src/compile.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<O: OutputStream> Compiler<O> {
262262
return Err(CompileError {
263263
statement: None,
264264
error: CompileErrorType::ExpectExpr,
265-
location: statement.location.clone(),
265+
location: statement.location,
266266
source_path: None,
267267
});
268268
}
@@ -306,7 +306,7 @@ impl<O: OutputStream> Compiler<O> {
306306

307307
fn compile_statement(&mut self, statement: &ast::Statement) -> Result<(), CompileError> {
308308
trace!("Compiling {:?}", statement);
309-
self.set_source_location(&statement.location);
309+
self.set_source_location(statement.location);
310310
use ast::StatementType::*;
311311

312312
match &statement.node {
@@ -542,7 +542,7 @@ impl<O: OutputStream> Compiler<O> {
542542
return Err(CompileError {
543543
statement: None,
544544
error: CompileErrorType::InvalidBreak,
545-
location: statement.location.clone(),
545+
location: statement.location,
546546
source_path: None,
547547
});
548548
}
@@ -553,7 +553,7 @@ impl<O: OutputStream> Compiler<O> {
553553
return Err(CompileError {
554554
statement: None,
555555
error: CompileErrorType::InvalidContinue,
556-
location: statement.location.clone(),
556+
location: statement.location,
557557
source_path: None,
558558
});
559559
}
@@ -564,7 +564,7 @@ impl<O: OutputStream> Compiler<O> {
564564
return Err(CompileError {
565565
statement: None,
566566
error: CompileErrorType::InvalidReturn,
567-
location: statement.location.clone(),
567+
location: statement.location,
568568
source_path: None,
569569
});
570570
}
@@ -643,7 +643,7 @@ impl<O: OutputStream> Compiler<O> {
643643
return Err(CompileError {
644644
statement: None,
645645
error: CompileErrorType::Delete(expression.name()),
646-
location: self.current_source_location.clone(),
646+
location: self.current_source_location,
647647
source_path: None,
648648
});
649649
}
@@ -1348,7 +1348,7 @@ impl<O: OutputStream> Compiler<O> {
13481348
return Err(CompileError {
13491349
statement: None,
13501350
error: CompileErrorType::StarArgs,
1351-
location: self.current_source_location.clone(),
1351+
location: self.current_source_location,
13521352
source_path: None,
13531353
});
13541354
} else {
@@ -1379,7 +1379,7 @@ impl<O: OutputStream> Compiler<O> {
13791379
return Err(CompileError {
13801380
statement: None,
13811381
error: CompileErrorType::Assign(target.name()),
1382-
location: self.current_source_location.clone(),
1382+
location: self.current_source_location,
13831383
source_path: None,
13841384
});
13851385
}
@@ -1570,7 +1570,7 @@ impl<O: OutputStream> Compiler<O> {
15701570

15711571
fn compile_expression(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
15721572
trace!("Compiling {:?}", expression);
1573-
self.set_source_location(&expression.location);
1573+
self.set_source_location(expression.location);
15741574

15751575
use ast::ExpressionType::*;
15761576
match &expression.node {
@@ -1665,7 +1665,7 @@ impl<O: OutputStream> Compiler<O> {
16651665
return Err(CompileError {
16661666
statement: Option::None,
16671667
error: CompileErrorType::InvalidYield,
1668-
location: self.current_source_location.clone(),
1668+
location: self.current_source_location,
16691669
source_path: Option::None,
16701670
});
16711671
}
@@ -1763,7 +1763,7 @@ impl<O: OutputStream> Compiler<O> {
17631763
error: CompileErrorType::SyntaxError(std::string::String::from(
17641764
"Invalid starred expression",
17651765
)),
1766-
location: self.current_source_location.clone(),
1766+
location: self.current_source_location,
17671767
source_path: Option::None,
17681768
});
17691769
}
@@ -2182,8 +2182,8 @@ impl<O: OutputStream> Compiler<O> {
21822182
self.current_output().set_label(label)
21832183
}
21842184

2185-
fn set_source_location(&mut self, location: &ast::Location) {
2186-
self.current_source_location = location.clone();
2185+
fn set_source_location(&mut self, location: ast::Location) {
2186+
self.current_source_location = location;
21872187
}
21882188

21892189
fn get_source_line_number(&mut self) -> usize {

parser/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl fmt::Display for ParseErrorType {
193193
ParseErrorType::UnrecognizedToken(ref tok, ref expected) => {
194194
if *tok == Tok::Indent {
195195
write!(f, "unexpected indent")
196-
} else if expected.clone() == Some("Indent".to_owned()) {
196+
} else if expected.as_deref() == Some("Indent") {
197197
write!(f, "expected an indented block")
198198
} else {
199199
write!(f, "Got unexpected token {}", tok)

parser/src/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn parse_params(
2222
// have defaults
2323
return Err(LexicalError {
2424
error: LexicalErrorType::DefaultArgumentError,
25-
location: name.location.clone(),
25+
location: name.location,
2626
});
2727
}
2828
}

parser/src/lexer.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ where
285285
let end_pos = self.get_pos();
286286
let value = BigInt::from_str_radix(&value_text, radix).map_err(|e| LexicalError {
287287
error: LexicalErrorType::OtherError(format!("{:?}", e)),
288-
location: start_pos.clone(),
288+
location: start_pos,
289289
})?;
290290
Ok((start_pos, Tok::Int { value }, end_pos))
291291
}
@@ -711,9 +711,8 @@ where
711711
Ordering::Greater => {
712712
// New indentation level:
713713
self.indentation_stack.push(indentation_level);
714-
let tok_start = self.get_pos();
715-
let tok_end = tok_start.clone();
716-
self.emit((tok_start, Tok::Indent, tok_end));
714+
let tok_pos = self.get_pos();
715+
self.emit((tok_pos, Tok::Indent, tok_pos));
717716
}
718717
Ordering::Less => {
719718
// One or more dedentations
@@ -726,9 +725,8 @@ where
726725
match ordering {
727726
Ordering::Less => {
728727
self.indentation_stack.pop();
729-
let tok_start = self.get_pos();
730-
let tok_end = tok_start.clone();
731-
self.emit((tok_start, Tok::Dedent, tok_end));
728+
let tok_pos = self.get_pos();
729+
self.emit((tok_pos, Tok::Dedent, tok_pos));
732730
}
733731
Ordering::Equal => {
734732
// We arrived at proper level of indentation.
@@ -786,16 +784,16 @@ where
786784
// Next, insert a trailing newline, if required.
787785
if !self.at_begin_of_line {
788786
self.at_begin_of_line = true;
789-
self.emit((tok_pos.clone(), Tok::Newline, tok_pos.clone()));
787+
self.emit((tok_pos, Tok::Newline, tok_pos));
790788
}
791789

792790
// Next, flush the indentation stack to zero.
793791
while self.indentation_stack.len() > 1 {
794792
self.indentation_stack.pop();
795-
self.emit((tok_pos.clone(), Tok::Dedent, tok_pos.clone()));
793+
self.emit((tok_pos, Tok::Dedent, tok_pos));
796794
}
797795

798-
self.emit((tok_pos.clone(), Tok::EndOfFile, tok_pos));
796+
self.emit((tok_pos, Tok::EndOfFile, tok_pos));
799797
}
800798

801799
Ok(())
@@ -1194,7 +1192,7 @@ where
11941192

11951193
/// Helper function to retrieve the current position.
11961194
fn get_pos(&self) -> Location {
1197-
self.location.clone()
1195+
self.location
11981196
}
11991197

12001198
/// Helper function to emit a lexed token to the queue of tokens.

parser/src/location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fmt;
44

55
/// A location somewhere in the sourcecode.
6-
#[derive(Clone, Debug, Default, PartialEq)]
6+
#[derive(Clone, Copy, Debug, Default, PartialEq)]
77
pub struct Location {
88
row: usize,
99
column: usize,

parser/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ mod tests {
138138

139139
fn as_statement(expr: ast::Expression) -> ast::Statement {
140140
ast::Statement {
141-
location: expr.location.clone(),
141+
location: expr.location,
142142
node: ast::StatementType::Expression { expression: expr },
143143
}
144144
}

parser/src/python.lalrpop

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,9 @@ SubscriptList: ast::Expression = {
874874
Subscript: ast::Expression = {
875875
Test,
876876
<e1:Test?> <location:@L> ":" <e2:Test?> <e3:SliceOp?> => {
877-
let s1 = e1.unwrap_or(ast::Expression { location: location.clone(), node: ast::ExpressionType::None });
878-
let s2 = e2.unwrap_or(ast::Expression { location: location.clone(), node: ast::ExpressionType::None });
879-
let s3 = e3.unwrap_or(ast::Expression { location: location.clone(), node: ast::ExpressionType::None });
877+
let s1 = e1.unwrap_or(ast::Expression { location, node: ast::ExpressionType::None });
878+
let s2 = e2.unwrap_or(ast::Expression { location, node: ast::ExpressionType::None });
879+
let s3 = e3.unwrap_or(ast::Expression { location, node: ast::ExpressionType::None });
880880
ast::Expression {
881881
location,
882882
node: ast::ExpressionType::Slice { elements: vec![s1, s2, s3] }
@@ -1061,7 +1061,7 @@ FunctionArgument: (Option<Option<String>>, ast::Expression) = {
10611061
<e:Test> <c:CompFor?> => {
10621062
let expr = match c {
10631063
Some(c) => ast::Expression {
1064-
location: e.location.clone(),
1064+
location: e.location,
10651065
node: ast::ExpressionType::Comprehension {
10661066
kind: Box::new(ast::ComprehensionKind::GeneratorExpression { element: e }),
10671067
generators: c,
@@ -1071,7 +1071,7 @@ FunctionArgument: (Option<Option<String>>, ast::Expression) = {
10711071
};
10721072
(None, expr)
10731073
},
1074-
<i:Identifier> "=" <e:Test> => (Some(Some(i.clone())), e),
1074+
<i:Identifier> "=" <e:Test> => (Some(Some(i)), e),
10751075
<location:@L> "*" <e:Test> => (None, ast::Expression { location, node: ast::ExpressionType::Starred { value: Box::new(e) } }),
10761076
"**" <e:Test> => (Some(None), e),
10771077
};
@@ -1105,7 +1105,7 @@ StringGroup: ast::StringGroup = {
11051105
let mut values = vec![];
11061106
for (value, is_fstring) in s {
11071107
values.push(if is_fstring {
1108-
parse_located_fstring(&value, loc.clone())?
1108+
parse_located_fstring(&value, loc)?
11091109
} else {
11101110
ast::StringGroup::Constant { value }
11111111
})

0 commit comments

Comments
 (0)