Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
&str::to_string -> &str::to_owned for literals
  • Loading branch information
youknowone committed Feb 5, 2020
commit 1bac582921dc8cef7477958aea5d1cb583402f92
4 changes: 2 additions & 2 deletions benchmarks/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn bench_rustpy_nbody(b: &mut test::Bencher) {

let vm = VirtualMachine::default();

let code = match vm.compile(source, compile::Mode::Single, "<stdin>".to_string()) {
let code = match vm.compile(source, compile::Mode::Single, "<stdin>".to_owned()) {
Ok(code) => code,
Err(e) => panic!("{:?}", e),
};
Expand All @@ -113,7 +113,7 @@ fn bench_rustpy_mandelbrot(b: &mut test::Bencher) {

let vm = VirtualMachine::default();

let code = match vm.compile(source, compile::Mode::Single, "<stdin>".to_string()) {
let code = match vm.compile(source, compile::Mode::Single, "<stdin>".to_owned()) {
Ok(code) => code,
Err(e) => panic!("{:?}", e),
};
Expand Down
22 changes: 11 additions & 11 deletions compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn with_compiler(
) -> Result<CodeObject, CompileError> {
let mut compiler = Compiler::new(optimize);
compiler.source_path = Some(source_path);
compiler.push_new_code_object("<module>".to_string());
compiler.push_new_code_object("<module>".to_owned());
f(&mut compiler)?;
let code = compiler.pop_code_object();
trace!("Compilation completed: {:?}", code);
Expand Down Expand Up @@ -897,7 +897,7 @@ impl<O: OutputStream> Compiler<O> {
// key:
self.emit(Instruction::LoadConst {
value: bytecode::Constant::String {
value: "return".to_string(),
value: "return".to_owned(),
},
});
// value:
Expand Down Expand Up @@ -989,11 +989,11 @@ impl<O: OutputStream> Compiler<O> {
let (new_body, doc_str) = get_doc(body);

self.emit(Instruction::LoadName {
name: "__name__".to_string(),
name: "__name__".to_owned(),
scope: bytecode::NameScope::Global,
});
self.emit(Instruction::StoreName {
name: "__module__".to_string(),
name: "__module__".to_owned(),
scope: bytecode::NameScope::Free,
});
self.emit(Instruction::LoadConst {
Expand All @@ -1002,7 +1002,7 @@ impl<O: OutputStream> Compiler<O> {
},
});
self.emit(Instruction::StoreName {
name: "__qualname__".to_string(),
name: "__qualname__".to_owned(),
scope: bytecode::NameScope::Free,
});
self.compile_statements(new_body)?;
Expand Down Expand Up @@ -1090,7 +1090,7 @@ impl<O: OutputStream> Compiler<O> {

self.emit(Instruction::Rotate { amount: 2 });
self.emit(Instruction::StoreAttr {
name: "__doc__".to_string(),
name: "__doc__".to_owned(),
});
}

Expand Down Expand Up @@ -1171,7 +1171,7 @@ impl<O: OutputStream> Compiler<O> {
self.set_label(check_asynciter_label);
self.emit(Instruction::Duplicate);
self.emit(Instruction::LoadName {
name: "StopAsyncIteration".to_string(),
name: "StopAsyncIteration".to_owned(),
scope: bytecode::NameScope::Global,
});
self.emit(Instruction::CompareOperation {
Expand Down Expand Up @@ -1732,7 +1732,7 @@ impl<O: OutputStream> Compiler<O> {
func: FunctionContext::Function,
};

let name = "<lambda>".to_string();
let name = "<lambda>".to_owned();
self.enter_function(&name, args)?;
self.compile_expression(body)?;
self.emit(Instruction::ReturnValue);
Expand Down Expand Up @@ -1933,7 +1933,7 @@ impl<O: OutputStream> Compiler<O> {
// Create magnificent function <listcomp>:
self.push_output(CodeObject::new(
Default::default(),
vec![".0".to_string()],
vec![".0".to_owned()],
Varargs::None,
vec![],
Varargs::None,
Expand Down Expand Up @@ -2264,8 +2264,8 @@ mod tests {

fn compile_exec(source: &str) -> CodeObject {
let mut compiler: Compiler = Default::default();
compiler.source_path = Some("source_path".to_string());
compiler.push_new_code_object("<module>".to_string());
compiler.source_path = Some("source_path".to_owned());
compiler.push_new_code_object("<module>".to_owned());
let ast = parser::parse_program(&source.to_string()).unwrap();
let symbol_scope = make_symbol_table(&ast).unwrap();
compiler.compile_program(&ast, symbol_scope).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl CompileError {
match parse {
ParseErrorType::Lexical(LexicalErrorType::IndentationError) => true,
ParseErrorType::UnrecognizedToken(token, expected) => {
*token == Tok::Indent || expected.clone() == Some("Indent".to_string())
*token == Tok::Indent || expected.clone() == Some("Indent".to_owned())
}
_ => false,
}
Expand All @@ -88,14 +88,14 @@ impl fmt::Display for CompileError {
let error_desc = match &self.error {
CompileErrorType::Assign(target) => format!("can't assign to {}", target),
CompileErrorType::Delete(target) => format!("can't delete {}", target),
CompileErrorType::ExpectExpr => "Expecting expression, got statement".to_string(),
CompileErrorType::ExpectExpr => "Expecting expression, got statement".to_owned(),
CompileErrorType::Parse(err) => err.to_string(),
CompileErrorType::SyntaxError(err) => err.to_string(),
CompileErrorType::StarArgs => "Two starred expressions in assignment".to_string(),
CompileErrorType::InvalidBreak => "'break' outside loop".to_string(),
CompileErrorType::InvalidContinue => "'continue' outside loop".to_string(),
CompileErrorType::InvalidReturn => "'return' outside function".to_string(),
CompileErrorType::InvalidYield => "'yield' outside function".to_string(),
CompileErrorType::StarArgs => "Two starred expressions in assignment".to_owned(),
CompileErrorType::InvalidBreak => "'break' outside loop".to_owned(),
CompileErrorType::InvalidContinue => "'continue' outside loop".to_owned(),
CompileErrorType::InvalidReturn => "'return' outside function".to_owned(),
CompileErrorType::InvalidYield => "'yield' outside function".to_owned(),
};

if let Some(statement) = &self.statement {
Expand Down
2 changes: 1 addition & 1 deletion derive/src/compile_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl PyCompileInput {
})?
.compile(
mode.unwrap_or(compile::Mode::Exec),
module_name.unwrap_or_else(|| "frozen".to_string()),
module_name.unwrap_or_else(|| "frozen".to_owned()),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion derive/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Class {
} else {
Err(Diagnostic::span_error(
span,
"Duplicate #[py*] attribute on pyimpl".to_string(),
"Duplicate #[py*] attribute on pyimpl".to_owned(),
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() -> vm::pyobject::PyResult<()> {
.compile(
r#"print("Hello World!")"#,
compiler::compile::Mode::Exec,
"<embedded>".to_string(),
"<embedded>".to_owned(),
)
.map_err(|err| vm.new_syntax_error(&err))?;

Expand Down
2 changes: 1 addition & 1 deletion examples/mini_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn main() -> vm::pyobject::PyResult<()> {
.compile(
&input,
compiler::compile::Mode::Single,
"<embedded>".to_string(),
"<embedded>".to_owned(),
)
.map_err(|err| vm.new_syntax_error(&err))
.and_then(|code_obj| vm.run_code_obj(code_obj, scope.clone()))
Expand Down
2 changes: 1 addition & 1 deletion examples/parse_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn parse_python_file(filename: &Path) -> ParsedFile {
match std::fs::read_to_string(filename) {
Err(e) => ParsedFile {
// filename: Box::new(filename.to_path_buf()),
// code: "".to_string(),
// code: "".to_owned(),
num_lines: 0,
result: Err(e.to_string()),
},
Expand Down
2 changes: 1 addition & 1 deletion parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl fmt::Display for ParseErrorType {
ParseErrorType::UnrecognizedToken(ref tok, ref expected) => {
if *tok == Tok::Indent {
write!(f, "unexpected indent")
} else if expected.clone() == Some("Indent".to_string()) {
} else if expected.clone() == Some("Indent".to_owned()) {
write!(f, "expected an indented block")
} else {
write!(f, "Got unexpected token {}", tok)
Expand Down
2 changes: 1 addition & 1 deletion parser/src/fstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ mod tests {
value: Box::new(mk_ident("foo", 1, 1)),
conversion: None,
spec: Some(Box::new(Constant {
value: "spec".to_string(),
value: "spec".to_owned(),
})),
}
);
Expand Down
12 changes: 6 additions & 6 deletions parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ where
if self.chr0 == Some('.') {
if self.chr1 == Some('_') {
return Err(LexicalError {
error: LexicalErrorType::OtherError("Invalid Syntax".to_string()),
error: LexicalErrorType::OtherError("Invalid Syntax".to_owned()),
location: self.get_pos(),
});
}
Expand Down Expand Up @@ -352,7 +352,7 @@ where
let value = value_text.parse::<BigInt>().unwrap();
if start_is_zero && !value.is_zero() {
return Err(LexicalError {
error: LexicalErrorType::OtherError("Invalid Token".to_string()),
error: LexicalErrorType::OtherError("Invalid Token".to_owned()),
location: self.get_pos(),
});
}
Expand Down Expand Up @@ -643,7 +643,7 @@ where
// This is technically stricter than python3 but spaces after
// tabs is even more insane than mixing spaces and tabs.
return Some(Err(LexicalError {
error: LexicalErrorType::OtherError("Spaces not allowed as part of indentation after tabs".to_string()),
error: LexicalErrorType::OtherError("Spaces not allowed as part of indentation after tabs".to_owned()),
location: self.get_pos(),
}));
}
Expand All @@ -658,7 +658,7 @@ where
// tabs is even more insane than mixing spaces and tabs.
return Err(LexicalError {
error: LexicalErrorType::OtherError(
"Tabs not allowed as part of indentation after spaces".to_string(),
"Tabs not allowed as part of indentation after spaces".to_owned(),
),
location: self.get_pos(),
});
Expand Down Expand Up @@ -1313,11 +1313,11 @@ mod tests {
tokens,
vec![
Tok::String {
value: "\\\\".to_string(),
value: "\\\\".to_owned(),
is_fstring: false,
},
Tok::String {
value: "\\".to_string(),
value: "\\".to_owned(),
is_fstring: false,
},
Tok::Newline,
Expand Down
2 changes: 1 addition & 1 deletion parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ mod tests {
function: Box::new(mk_ident("my_func", 1, 1)),
args: vec![make_string("positional", 1, 10)],
keywords: vec![ast::Keyword {
name: Some("keyword".to_string()),
name: Some("keyword".to_owned()),
value: make_int(2, 1, 31),
}],
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ fn _run_string(vm: &VirtualMachine, scope: Scope, source: &str, source_path: Str

fn run_command(vm: &VirtualMachine, scope: Scope, source: String) -> PyResult<()> {
debug!("Running command {}", source);
_run_string(vm, scope, &source, "<stdin>".to_string())?;
_run_string(vm, scope, &source, "<stdin>".to_owned())?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum ShellExecResult {
}

fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResult {
match vm.compile(source, compile::Mode::Single, "<stdin>".to_string()) {
match vm.compile(source, compile::Mode::Single, "<stdin>".to_owned()) {
Ok(code) => {
match vm.run_code_obj(code, scope.clone()) {
Ok(value) => {
Expand Down
2 changes: 1 addition & 1 deletion src/shell/rustyline_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Completer for ShellHelper<'_> {
.complete_opt(&line[0..pos])
// as far as I can tell, there's no better way to do both completion
// and indentation (or even just indentation)
.unwrap_or_else(|| (line.len(), vec!["\t".to_string()])))
.unwrap_or_else(|| (line.len(), vec!["\t".to_owned()])))
}
}

Expand Down
22 changes: 11 additions & 11 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn builtin_callable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
fn builtin_chr(i: u32, vm: &VirtualMachine) -> PyResult<String> {
match char::from_u32(i) {
Some(value) => Ok(value.to_string()),
None => Err(vm.new_value_error("chr() arg not in range(0x110000)".to_string())),
None => Err(vm.new_value_error("chr() arg not in range(0x110000)".to_owned())),
}
}

Expand Down Expand Up @@ -229,7 +229,7 @@ fn run_code(
// Determine code object:
let code_obj = match source {
Either::A(string) => vm
.compile(string.as_str(), mode, "<string>".to_string())
.compile(string.as_str(), mode, "<string>".to_owned())
.map_err(|err| vm.new_syntax_error(&err))?,
Either::B(code_obj) => code_obj,
};
Expand Down Expand Up @@ -400,14 +400,14 @@ fn builtin_max(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
std::cmp::Ordering::Equal => vm.extract_elements(&args.args[0])?,
std::cmp::Ordering::Less => {
// zero arguments means type error:
return Err(vm.new_type_error("Expected 1 or more arguments".to_string()));
return Err(vm.new_type_error("Expected 1 or more arguments".to_owned()));
}
};

if candidates.is_empty() {
let default = args.get_optional_kwarg("default");
return default
.ok_or_else(|| vm.new_value_error("max() arg is an empty sequence".to_string()));
.ok_or_else(|| vm.new_value_error("max() arg is an empty sequence".to_owned()));
}

let key_func = args.get_optional_kwarg("key");
Expand Down Expand Up @@ -446,14 +446,14 @@ fn builtin_min(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
std::cmp::Ordering::Equal => vm.extract_elements(&args.args[0])?,
std::cmp::Ordering::Less => {
// zero arguments means type error:
return Err(vm.new_type_error("Expected 1 or more arguments".to_string()));
return Err(vm.new_type_error("Expected 1 or more arguments".to_owned()));
}
};

if candidates.is_empty() {
let default = args.get_optional_kwarg("default");
return default
.ok_or_else(|| vm.new_value_error("min() arg is an empty sequence".to_string()));
.ok_or_else(|| vm.new_value_error("min() arg is an empty sequence".to_owned()));
}

let key_func = args.get_optional_kwarg("key");
Expand Down Expand Up @@ -540,7 +540,7 @@ fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) ->
match string.chars().next() {
Some(character) => Ok(character as u32),
None => Err(vm.new_type_error(
"ord() could not guess the integer representing this character".to_string(),
"ord() could not guess the integer representing this character".to_owned(),
)),
}
}
Expand All @@ -565,18 +565,18 @@ fn builtin_pow(
&& objtype::isinstance(&y, &vm.ctx.int_type()))
{
return Err(vm.new_type_error(
"pow() 3rd argument not allowed unless all arguments are integers".to_string(),
"pow() 3rd argument not allowed unless all arguments are integers".to_owned(),
));
}
let y = objint::get_value(&y);
if y.sign() == Sign::Minus {
return Err(vm.new_value_error(
"pow() 2nd argument cannot be negative when 3rd argument specified".to_string(),
"pow() 2nd argument cannot be negative when 3rd argument specified".to_owned(),
));
}
let m = m.as_bigint();
if m.is_zero() {
return Err(vm.new_value_error("pow() 3rd argument cannot be 0".to_string()));
return Err(vm.new_value_error("pow() 3rd argument cannot be 0".to_owned()));
}
let x = objint::get_value(&x);
Ok(vm.new_int(x.modpow(&y, &m)))
Expand Down Expand Up @@ -675,7 +675,7 @@ fn builtin_reversed(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
vm.invoke(&reversed_method?, PyFuncArgs::default())
} else {
vm.get_method_or_type_error(obj.clone(), "__getitem__", || {
"argument to reversed() must be a sequence".to_string()
"argument to reversed() must be a sequence".to_owned()
})?;
let len = vm.call_method(&obj.clone(), "__len__", PyFuncArgs::default())?;
let obj_iterator = objiter::PySequenceIterator {
Expand Down
Loading