Skip to content

Commit 7096434

Browse files
committed
Implement some clippy tips
1 parent 55aa12b commit 7096434

File tree

8 files changed

+95
-91
lines changed

8 files changed

+95
-91
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn main() {
7070
}
7171

7272
fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: Option<String>) -> PyResult {
73-
let code_obj = compile::compile(vm, source, compile::Mode::Exec, source_path)?;
73+
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path)?;
7474
// trace!("Code object: {:?}", code_obj.borrow());
7575
let builtins = vm.get_builtin_scope();
7676
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
@@ -115,7 +115,7 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
115115
}
116116

117117
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool {
118-
match compile::compile(vm, source, compile::Mode::Single, None) {
118+
match compile::compile(vm, source, &compile::Mode::Single, None) {
119119
Ok(code) => {
120120
match vm.run_code_obj(code, scope) {
121121
Ok(_value) => {

vm/src/builtins.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn builtin_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
151151

152152
let filename = objstr::get_value(filename);
153153

154-
compile::compile(vm, &source, mode, Some(filename))
154+
compile::compile(vm, &source, &mode, Some(filename))
155155
}
156156

157157
fn builtin_delattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -203,6 +203,8 @@ fn builtin_enumerate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
203203
Ok(vm.ctx.new_list(new_items))
204204
}
205205

206+
/// Implements `eval`.
207+
/// See also: https://docs.python.org/3/library/functions.html#eval
206208
fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
207209
arg_check!(
208210
vm,
@@ -222,7 +224,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
222224
let source = objstr::get_value(source);
223225
// TODO: fix this newline bug:
224226
let source = format!("{}\n", source);
225-
compile::compile(vm, &source, mode, None)?
227+
compile::compile(vm, &source, &mode, None)?
226228
} else {
227229
return Err(vm.new_type_error("code argument must be str or code object".to_string()));
228230
};
@@ -249,6 +251,8 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
249251
vm.run_code_obj(code_obj.clone(), scope)
250252
}
251253

254+
/// Implements `exec`
255+
/// https://docs.python.org/3/library/functions.html#exec
252256
fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
253257
arg_check!(
254258
vm,
@@ -266,7 +270,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
266270
let source = objstr::get_value(source);
267271
// TODO: fix this newline bug:
268272
let source = format!("{}\n", source);
269-
compile::compile(vm, &source, mode, None)?
273+
compile::compile(vm, &source, &mode, None)?
270274
} else if objtype::isinstance(source, &vm.ctx.code_type()) {
271275
source.clone()
272276
} else {

vm/src/compile.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct Compiler {
2323
pub fn compile(
2424
vm: &mut VirtualMachine,
2525
source: &str,
26-
mode: Mode,
26+
mode: &Mode,
2727
source_path: Option<String>,
2828
) -> PyResult {
2929
let mut compiler = Compiler::new();
@@ -112,7 +112,7 @@ impl Compiler {
112112

113113
fn compile_program_single(&mut self, program: &ast::Program) -> Result<(), String> {
114114
for statement in &program.statements {
115-
if let &ast::Statement::Expression { ref expression } = &statement.node {
115+
if let ast::Statement::Expression { ref expression } = statement.node {
116116
self.compile_expression(expression)?;
117117
self.emit(Instruction::PrintExpr);
118118
} else {
@@ -128,7 +128,7 @@ impl Compiler {
128128

129129
// Compile statement in eval mode:
130130
fn compile_statement_eval(&mut self, statement: &ast::LocatedStatement) -> Result<(), String> {
131-
if let &ast::Statement::Expression { ref expression } = &statement.node {
131+
if let ast::Statement::Expression { ref expression } = statement.node {
132132
self.compile_expression(expression)?;
133133
self.emit(Instruction::ReturnValue);
134134
Ok(())
@@ -500,7 +500,7 @@ impl Compiler {
500500
self.compile_expression(base)?;
501501
}
502502

503-
if keywords.len() > 0 {
503+
if !keywords.is_empty() {
504504
let mut kwarg_names = vec![];
505505
for keyword in keywords {
506506
if let Some(name) = &keyword.name {
@@ -627,7 +627,7 @@ impl Compiler {
627627
self.emit(Instruction::DeleteSubscript);
628628
}
629629
_ => {
630-
return Err(format!("Invalid delete statement"));
630+
return Err("Invalid delete statement".to_string());
631631
}
632632
}
633633
}
@@ -641,10 +641,10 @@ impl Compiler {
641641

642642
fn enter_function(
643643
&mut self,
644-
name: &String,
644+
name: &str,
645645
args: &ast::Parameters,
646646
) -> Result<bytecode::FunctionOpArg, String> {
647-
let have_kwargs = args.defaults.len() > 0;
647+
let have_kwargs = !args.defaults.is_empty();
648648
if have_kwargs {
649649
// Construct a tuple:
650650
let size = args.defaults.len();
@@ -663,7 +663,7 @@ impl Compiler {
663663
args.kwonlyargs.clone(),
664664
args.kwarg.clone(),
665665
self.source_path.clone(),
666-
name.clone(),
666+
name.to_string(),
667667
));
668668

669669
let mut flags = bytecode::FunctionOpArg::empty();
@@ -780,15 +780,15 @@ impl Compiler {
780780
let f = false_label.unwrap_or_else(|| self.new_label());
781781
self.compile_test(a, None, Some(f), context)?;
782782
self.compile_test(b, true_label, false_label, context)?;
783-
if let None = false_label {
783+
if false_label.is_none() {
784784
self.set_label(f);
785785
}
786786
}
787787
ast::BooleanOperator::Or => {
788788
let t = true_label.unwrap_or_else(|| self.new_label());
789789
self.compile_test(a, Some(t), None, context)?;
790790
self.compile_test(b, true_label, false_label, context)?;
791-
if let None = true_label {
791+
if true_label.is_none() {
792792
self.set_label(t);
793793
}
794794
}
@@ -1054,7 +1054,7 @@ impl Compiler {
10541054
});
10551055

10561056
// Create an optional map with kw-args:
1057-
if keywords.len() > 0 {
1057+
if !keywords.is_empty() {
10581058
for keyword in keywords {
10591059
if let Some(name) = &keyword.name {
10601060
self.emit(Instruction::LoadConst {
@@ -1090,7 +1090,7 @@ impl Compiler {
10901090
}
10911091
} else {
10921092
// Keyword arguments:
1093-
if keywords.len() > 0 {
1093+
if !keywords.is_empty() {
10941094
let mut kwarg_names = vec![];
10951095
for keyword in keywords {
10961096
if let Some(name) = &keyword.name {

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::pyobject::{PyObjectRef, PyResult};
55
use super::vm::VirtualMachine;
66

77
pub fn eval(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> PyResult {
8-
match compile::compile(vm, source, compile::Mode::Eval, None) {
8+
match compile::compile(vm, source, &compile::Mode::Eval, None) {
99
Ok(bytecode) => {
1010
debug!("Code object: {:?}", bytecode);
1111
vm.run_code_obj(bytecode, scope)

vm/src/exceptions.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,35 +150,35 @@ impl ExceptionZoo {
150150
create_type("PermissionError", &type_type, &import_error, &dict_type);
151151

152152
ExceptionZoo {
153-
assertion_error: assertion_error,
154-
attribute_error: attribute_error,
155-
base_exception_type: base_exception_type,
156-
exception_type: exception_type,
157-
file_not_found_error: file_not_found_error,
158-
import_error: import_error,
159-
index_error: index_error,
160-
key_error: key_error,
161-
module_not_found_error: module_not_found_error,
162-
name_error: name_error,
163-
not_implemented_error: not_implemented_error,
164-
permission_error: permission_error,
165-
runtime_error: runtime_error,
166-
stop_iteration: stop_iteration,
167-
syntax_error: syntax_error,
168-
type_error: type_error,
169-
value_error: value_error,
153+
assertion_error,
154+
attribute_error,
155+
base_exception_type,
156+
exception_type,
157+
file_not_found_error,
158+
import_error,
159+
index_error,
160+
key_error,
161+
module_not_found_error,
162+
name_error,
163+
not_implemented_error,
164+
permission_error,
165+
runtime_error,
166+
stop_iteration,
167+
syntax_error,
168+
type_error,
169+
value_error,
170170
}
171171
}
172172
}
173173

174174
pub fn init(context: &PyContext) {
175-
let ref base_exception_type = context.exceptions.base_exception_type;
175+
let base_exception_type = &context.exceptions.base_exception_type;
176176
context.set_attr(
177177
&base_exception_type,
178178
"__init__",
179179
context.new_rustfunc(exception_init),
180180
);
181-
let ref exception_type = context.exceptions.exception_type;
181+
let exception_type = &context.exceptions.exception_type;
182182
context.set_attr(
183183
&exception_type,
184184
"__str__",

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn import_uncached_module(
3434
let code_obj = compile::compile(
3535
vm,
3636
&source,
37-
compile::Mode::Exec,
37+
&compile::Mode::Exec,
3838
Some(filepath.to_str().unwrap().to_string()),
3939
)?;
4040
// trace!("Code object: {:?}", code_obj);

vm/src/pyobject.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -219,37 +219,37 @@ impl PyContext {
219219
bool_type.clone(),
220220
);
221221
let context = PyContext {
222-
bool_type: bool_type,
223-
memoryview_type: memoryview_type,
224-
bytearray_type: bytearray_type,
225-
bytes_type: bytes_type,
226-
code_type: code_type,
227-
complex_type: complex_type,
228-
classmethod_type: classmethod_type,
229-
int_type: int_type,
230-
float_type: float_type,
231-
frame_type: frame_type,
232-
staticmethod_type: staticmethod_type,
233-
list_type: list_type,
234-
set_type: set_type,
235-
frozenset_type: frozenset_type,
236-
true_value: true_value,
237-
false_value: false_value,
238-
tuple_type: tuple_type,
239-
iter_type: iter_type,
240-
dict_type: dict_type,
222+
bool_type,
223+
memoryview_type,
224+
bytearray_type,
225+
bytes_type,
226+
code_type,
227+
complex_type,
228+
classmethod_type,
229+
int_type,
230+
float_type,
231+
frame_type,
232+
staticmethod_type,
233+
list_type,
234+
set_type,
235+
frozenset_type,
236+
true_value,
237+
false_value,
238+
tuple_type,
239+
iter_type,
240+
dict_type,
241241
none: none,
242-
str_type: str_type,
242+
str_type,
243243
object: object_type,
244-
function_type: function_type,
245-
super_type: super_type,
246-
property_type: property_type,
247-
generator_type: generator_type,
248-
module_type: module_type,
249-
bound_method_type: bound_method_type,
250-
member_descriptor_type: member_descriptor_type,
251-
type_type: type_type,
252-
exceptions: exceptions,
244+
function_type,
245+
super_type,
246+
property_type,
247+
generator_type,
248+
module_type,
249+
bound_method_type,
250+
member_descriptor_type,
251+
type_type,
252+
exceptions,
253253
};
254254
objtype::init(&context);
255255
objlist::init(&context);
@@ -930,43 +930,43 @@ pub enum PyObjectPayload {
930930
impl fmt::Debug for PyObjectPayload {
931931
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
932932
match self {
933-
&PyObjectPayload::String { ref value } => write!(f, "str \"{}\"", value),
934-
&PyObjectPayload::Integer { ref value } => write!(f, "int {}", value),
935-
&PyObjectPayload::Float { ref value } => write!(f, "float {}", value),
936-
&PyObjectPayload::Complex { ref value } => write!(f, "complex {}", value),
937-
&PyObjectPayload::Bytes { ref value } => write!(f, "bytes/bytearray {:?}", value),
938-
&PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
939-
&PyObjectPayload::Sequence { elements: _ } => write!(f, "list or tuple"),
940-
&PyObjectPayload::Dict { elements: _ } => write!(f, "dict"),
941-
&PyObjectPayload::Set { elements: _ } => write!(f, "set"),
942-
&PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
943-
&PyObjectPayload::Iterator {
933+
PyObjectPayload::String { ref value } => write!(f, "str \"{}\"", value),
934+
PyObjectPayload::Integer { ref value } => write!(f, "int {}", value),
935+
PyObjectPayload::Float { ref value } => write!(f, "float {}", value),
936+
PyObjectPayload::Complex { ref value } => write!(f, "complex {}", value),
937+
PyObjectPayload::Bytes { ref value } => write!(f, "bytes/bytearray {:?}", value),
938+
PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
939+
PyObjectPayload::Sequence { elements: _ } => write!(f, "list or tuple"),
940+
PyObjectPayload::Dict { elements: _ } => write!(f, "dict"),
941+
PyObjectPayload::Set { elements: _ } => write!(f, "set"),
942+
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
943+
PyObjectPayload::Iterator {
944944
position: _,
945945
iterated_obj: _,
946946
} => write!(f, "iterator"),
947-
&PyObjectPayload::Slice {
947+
PyObjectPayload::Slice {
948948
start: _,
949949
stop: _,
950950
step: _,
951951
} => write!(f, "slice"),
952-
&PyObjectPayload::Code { ref code } => write!(f, "code: {:?}", code),
953-
&PyObjectPayload::Function { .. } => write!(f, "function"),
954-
&PyObjectPayload::Generator { .. } => write!(f, "generator"),
955-
&PyObjectPayload::BoundMethod {
952+
PyObjectPayload::Code { ref code } => write!(f, "code: {:?}", code),
953+
PyObjectPayload::Function { .. } => write!(f, "function"),
954+
PyObjectPayload::Generator { .. } => write!(f, "generator"),
955+
PyObjectPayload::BoundMethod {
956956
ref function,
957957
ref object,
958958
} => write!(f, "bound-method: {:?} of {:?}", function, object),
959-
&PyObjectPayload::Module { name: _, dict: _ } => write!(f, "module"),
960-
&PyObjectPayload::Scope { scope: _ } => write!(f, "scope"),
961-
&PyObjectPayload::None => write!(f, "None"),
962-
&PyObjectPayload::Class {
959+
PyObjectPayload::Module { name: _, dict: _ } => write!(f, "module"),
960+
PyObjectPayload::Scope { scope: _ } => write!(f, "scope"),
961+
PyObjectPayload::None => write!(f, "None"),
962+
PyObjectPayload::Class {
963963
ref name,
964964
dict: _,
965965
mro: _,
966966
} => write!(f, "class {:?}", name),
967-
&PyObjectPayload::Instance { dict: _ } => write!(f, "instance"),
968-
&PyObjectPayload::RustFunction { function: _ } => write!(f, "rust function"),
969-
&PyObjectPayload::Frame { .. } => write!(f, "frame"),
967+
PyObjectPayload::Instance { dict: _ } => write!(f, "instance"),
968+
PyObjectPayload::RustFunction { function: _ } => write!(f, "rust function"),
969+
PyObjectPayload::Frame { .. } => write!(f, "frame"),
970970
}
971971
}
972972
}

wasm/lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn eval(vm: &mut VirtualMachine, source: &str, vars: PyObjectRef) -> PyResult {
123123
source.push('\n');
124124
}
125125

126-
let code_obj = compile::compile(vm, &source, compile::Mode::Exec, None)?;
126+
let code_obj = compile::compile(vm, &source, &compile::Mode::Exec, None)?;
127127

128128
vm.run_code_obj(code_obj, vars)
129129
}

0 commit comments

Comments
 (0)