Skip to content

Commit 3bc9b78

Browse files
committed
Remove redundant clone
1 parent 074579c commit 3bc9b78

20 files changed

+55
-73
lines changed

vm/src/builtins.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,9 @@ mod decl {
175175

176176
#[pyfunction]
177177
fn divmod(a: PyObjectRef, b: PyObjectRef, vm: &VirtualMachine) -> PyResult {
178-
vm.call_or_reflection(
179-
a.clone(),
180-
b.clone(),
181-
"__divmod__",
182-
"__rdivmod__",
183-
|vm, a, b| Err(vm.new_unsupported_operand_error(a, b, "divmod")),
184-
)
178+
vm.call_or_reflection(a, b, "__divmod__", "__rdivmod__", |vm, a, b| {
179+
Err(vm.new_unsupported_operand_error(a, b, "divmod"))
180+
})
185181
}
186182

187183
#[cfg(feature = "rustpython-compiler")]
@@ -307,7 +303,7 @@ mod decl {
307303
default: OptionalArg<PyObjectRef>,
308304
vm: &VirtualMachine,
309305
) -> PyResult {
310-
let ret = vm.get_attribute(obj.clone(), attr);
306+
let ret = vm.get_attribute(obj, attr);
311307
if let OptionalArg::Present(default) = default {
312308
ret.or_else(|ex| catch_attr_exception(ex, default, vm))
313309
} else {
@@ -322,7 +318,7 @@ mod decl {
322318

323319
#[pyfunction]
324320
fn hasattr(obj: PyObjectRef, attr: PyStringRef, vm: &VirtualMachine) -> PyResult<bool> {
325-
if let Err(ex) = vm.get_attribute(obj.clone(), attr) {
321+
if let Err(ex) = vm.get_attribute(obj, attr) {
326322
catch_attr_exception(ex, false, vm)
327323
} else {
328324
Ok(true)
@@ -533,7 +529,7 @@ mod decl {
533529
if objtype::isinstance(&value, &vm.ctx.exceptions.stop_iteration) {
534530
match default_value {
535531
OptionalArg::Missing => Err(value),
536-
OptionalArg::Present(value) => Ok(value.clone()),
532+
OptionalArg::Present(value) => Ok(value),
537533
}
538534
} else {
539535
Err(value)
@@ -595,7 +591,7 @@ mod decl {
595591
) -> PyResult {
596592
match mod_value {
597593
OptionalArg::Missing => {
598-
vm.call_or_reflection(x.clone(), y.clone(), "__pow__", "__rpow__", |vm, x, y| {
594+
vm.call_or_reflection(x, y, "__pow__", "__rpow__", |vm, x, y| {
599595
Err(vm.new_unsupported_operand_error(x, y, "pow"))
600596
})
601597
}

vm/src/bytesinner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl ByteInnerNewOptions {
8686
// Only one argument
8787
} else {
8888
let value = if let OptionalArg::Present(ival) = self.val_option {
89-
match_class!(match ival.clone() {
89+
match_class!(match ival {
9090
i @ PyInt => {
9191
let size =
9292
objint::get_value(&i.into_object())

vm/src/cformat.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,9 @@ impl CFormatSpec {
292292
let result = match preconversor {
293293
CFormatPreconversor::Str => vm.to_str(&obj)?,
294294
CFormatPreconversor::Repr | CFormatPreconversor::Ascii => vm.to_repr(&obj)?,
295-
CFormatPreconversor::Bytes => TryFromObject::try_from_object(
296-
vm,
297-
vm.call_method(&obj.clone(), "decode", vec![])?,
298-
)?,
295+
CFormatPreconversor::Bytes => {
296+
TryFromObject::try_from_object(vm, vm.call_method(&obj, "decode", vec![])?)?
297+
}
299298
};
300299
Ok(self.format_string(result.borrow_value().to_owned()))
301300
}

vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ impl ExecutingFrame<'_> {
14041404
needle: PyObjectRef,
14051405
haystack: PyObjectRef,
14061406
) -> PyResult<bool> {
1407-
let found = vm._membership(haystack.clone(), needle)?;
1407+
let found = vm._membership(haystack, needle)?;
14081408
Ok(objbool::boolval(vm, found)?)
14091409
}
14101410

@@ -1414,7 +1414,7 @@ impl ExecutingFrame<'_> {
14141414
needle: PyObjectRef,
14151415
haystack: PyObjectRef,
14161416
) -> PyResult<bool> {
1417-
let found = vm._membership(haystack.clone(), needle)?;
1417+
let found = vm._membership(haystack, needle)?;
14181418
Ok(!objbool::boolval(vm, found)?)
14191419
}
14201420

vm/src/import.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ pub fn init_importlib(vm: &mut VirtualMachine, initialize_parameter: InitParamet
2525
match initialize_parameter {
2626
InitParameter::InitializeExternal if cfg!(feature = "rustpython-compiler") => {
2727
flame_guard!("install_external");
28-
let install_external =
29-
vm.get_attribute(importlib.clone(), "_install_external_importers")?;
28+
let install_external = vm.get_attribute(importlib, "_install_external_importers")?;
3029
vm.invoke(&install_external, vec![])?;
3130
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
3231
let importlib_external = vm.import("_frozen_importlib_external", &[], 0)?;

vm/src/obj/objbool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn boolval(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
5353

5454
get_value(&bool_obj)
5555
}
56-
None => match vm.get_method(obj.clone(), "__len__") {
56+
None => match vm.get_method(obj, "__len__") {
5757
Some(method_or_err) => {
5858
let method = method_or_err?;
5959
let bool_obj = vm.invoke(&method, PyFuncArgs::default())?;
@@ -120,7 +120,7 @@ impl PyBool {
120120
let rhs = get_value(&rhs);
121121
(lhs || rhs).into_pyobject(vm)
122122
} else {
123-
get_py_int(&lhs).or(rhs.clone(), vm).into_pyobject(vm)
123+
get_py_int(&lhs).or(rhs, vm).into_pyobject(vm)
124124
}
125125
}
126126

@@ -134,7 +134,7 @@ impl PyBool {
134134
let rhs = get_value(&rhs);
135135
(lhs && rhs).into_pyobject(vm)
136136
} else {
137-
get_py_int(&lhs).and(rhs.clone(), vm).into_pyobject(vm)
137+
get_py_int(&lhs).and(rhs, vm).into_pyobject(vm)
138138
}
139139
}
140140

@@ -148,7 +148,7 @@ impl PyBool {
148148
let rhs = get_value(&rhs);
149149
(lhs ^ rhs).into_pyobject(vm)
150150
} else {
151-
get_py_int(&lhs).xor(rhs.clone(), vm).into_pyobject(vm)
151+
get_py_int(&lhs).xor(rhs, vm).into_pyobject(vm)
152152
}
153153
}
154154

@@ -162,7 +162,7 @@ impl PyBool {
162162
)));
163163
}
164164
let val = match x {
165-
OptionalArg::Present(val) => boolval(vm, val.clone())?,
165+
OptionalArg::Present(val) => boolval(vm, val)?,
166166
OptionalArg::Missing => false,
167167
};
168168
Ok(vm.ctx.new_bool(val))

vm/src/obj/objclassmethod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ impl PyClassMethod {
6666
callable: PyObjectRef,
6767
vm: &VirtualMachine,
6868
) -> PyResult<PyClassMethodRef> {
69-
PyClassMethod {
70-
callable: callable.clone(),
71-
}
72-
.into_ref_with_type(vm, cls)
69+
PyClassMethod { callable }.into_ref_with_type(vm, cls)
7370
}
7471

7572
#[pyproperty(name = "__func__")]

vm/src/obj/objdict.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl PyDictRef {
341341

342342
#[pymethod(name = "__ior__")]
343343
fn ior(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
344-
let dicted: Result<PyDictRef, _> = other.clone().downcast();
344+
let dicted: Result<PyDictRef, _> = other.downcast();
345345
if let Ok(other) = dicted {
346346
PyDictRef::merge_dict(&self.entries, other, vm)?;
347347
return Ok(self.into_object());
@@ -351,7 +351,7 @@ impl PyDictRef {
351351

352352
#[pymethod(name = "__ror__")]
353353
fn ror(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDict> {
354-
let dicted: Result<PyDictRef, _> = other.clone().downcast();
354+
let dicted: Result<PyDictRef, _> = other.downcast();
355355
if let Ok(other) = dicted {
356356
let other_cp = other.copy();
357357
PyDictRef::merge_dict(&other_cp.entries, self, vm)?;
@@ -362,7 +362,7 @@ impl PyDictRef {
362362

363363
#[pymethod(name = "__or__")]
364364
fn or(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDict> {
365-
let dicted: Result<PyDictRef, _> = other.clone().downcast();
365+
let dicted: Result<PyDictRef, _> = other.downcast();
366366
if let Ok(other) = dicted {
367367
let self_cp = self.copy();
368368
PyDictRef::merge_dict(&self_cp.entries, other, vm)?;

vm/src/obj/objfilter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl PyFilter {
3535
let iterator = objiter::get_iter(vm, &iterable)?;
3636

3737
PyFilter {
38-
predicate: function.clone(),
38+
predicate: function,
3939
iterator,
4040
}
4141
.into_ref_with_type(vm, cls)

vm/src/obj/objlist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl PyList {
131131
};
132132
// Bound it by [0, vec.len()]
133133
let position = unbounded_position.min(vec_len).to_usize().unwrap_or(0);
134-
elements.insert(position, element.clone());
134+
elements.insert(position, element);
135135
}
136136

137137
#[pymethod(name = "__add__")]
@@ -202,7 +202,7 @@ impl PyList {
202202

203203
#[pymethod(name = "__getitem__")]
204204
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
205-
get_item(vm, zelf.as_object(), &zelf.borrow_value(), needle.clone())
205+
get_item(vm, zelf.as_object(), &zelf.borrow_value(), needle)
206206
}
207207

208208
#[pymethod(name = "__iter__")]

vm/src/obj/objmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl PyMap {
3636
.map(|iterable| objiter::get_iter(vm, &iterable))
3737
.collect::<Result<Vec<_>, _>>()?;
3838
PyMap {
39-
mapper: function.clone(),
39+
mapper: function,
4040
iterators,
4141
}
4242
.into_ref_with_type(vm, cls)

vm/src/obj/objobject.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl PyBaseObject {
103103
fn delattr(obj: PyObjectRef, attr_name: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
104104
if let Some(attr) = obj.get_class_attr(attr_name.borrow_value()) {
105105
if let Some(descriptor) = attr.get_class_attr("__delete__") {
106-
return vm.invoke(&descriptor, vec![attr, obj.clone()]).map(|_| ());
106+
return vm.invoke(&descriptor, vec![attr, obj]).map(|_| ());
107107
}
108108
}
109109

@@ -251,9 +251,7 @@ pub(crate) fn setattr(
251251

252252
if let Some(attr) = obj.get_class_attr(attr_name.borrow_value()) {
253253
if let Some(descriptor) = attr.get_class_attr("__set__") {
254-
return vm
255-
.invoke(&descriptor, vec![attr, obj.clone(), value])
256-
.map(|_| ());
254+
return vm.invoke(&descriptor, vec![attr, obj, value]).map(|_| ());
257255
}
258256
}
259257

vm/src/obj/objstaticmethod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ impl PyStaticMethod {
4343
callable: PyObjectRef,
4444
vm: &VirtualMachine,
4545
) -> PyResult<PyStaticMethodRef> {
46-
PyStaticMethod {
47-
callable: callable.clone(),
48-
}
49-
.into_ref_with_type(vm, cls)
46+
PyStaticMethod { callable }.into_ref_with_type(vm, cls)
5047
}
5148
}
5249

vm/src/obj/objsuper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn supercheck(ty: PyClassRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult
171171
if objtype::isinstance(&obj, &ty) {
172172
return Ok(obj.class());
173173
}
174-
let class_attr = vm.get_attribute(obj.clone(), "__class__")?;
174+
let class_attr = vm.get_attribute(obj, "__class__")?;
175175
if let Ok(cls) = class_attr.downcast::<PyClass>() {
176176
if !cls.is(&ty) && objtype::issubclass(&cls, &ty) {
177177
return Ok(cls);

vm/src/obj/objtuple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl PyTuple {
208208

209209
#[pymethod(name = "__getitem__")]
210210
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
211-
get_item(vm, zelf.as_object(), &zelf.elements, needle.clone())
211+
get_item(vm, zelf.as_object(), &zelf.elements, needle)
212212
}
213213

214214
#[pymethod(name = "index")]

vm/src/stdlib/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn imp_is_frozen(name: PyStringRef, vm: &VirtualMachine) -> bool {
5757

5858
fn imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult {
5959
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
60-
let spec = vm.get_attribute(spec.clone(), "name")?;
60+
let spec = vm.get_attribute(spec, "name")?;
6161
let name = objstr::borrow_value(&spec);
6262

6363
if let Ok(module) = sys_modules.get_item(name, vm) {

vm/src/stdlib/io.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ fn bytes_io_new(
404404
}
405405

406406
fn io_base_cm_enter(instance: PyObjectRef) -> PyObjectRef {
407-
instance.clone()
407+
instance
408408
}
409409

410410
fn io_base_cm_del(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
@@ -576,7 +576,7 @@ fn buffered_io_base_init(
576576
buffer_size: OptionalArg<usize>,
577577
vm: &VirtualMachine,
578578
) -> PyResult<()> {
579-
vm.set_attr(&instance, "raw", raw.clone())?;
579+
vm.set_attr(&instance, "raw", raw)?;
580580
vm.set_attr(
581581
&instance,
582582
"buffer_size",
@@ -606,7 +606,7 @@ fn buffered_reader_read(
606606
vm: &VirtualMachine,
607607
) -> PyResult {
608608
vm.call_method(
609-
&vm.get_attribute(instance.clone(), "raw")?,
609+
&vm.get_attribute(instance, "raw")?,
610610
"read",
611611
vec![size.to_usize().into_pyobject(vm)],
612612
)
@@ -902,7 +902,7 @@ fn buffered_writer_write(instance: PyObjectRef, obj: PyObjectRef, vm: &VirtualMa
902902
let raw = vm.get_attribute(instance, "raw").unwrap();
903903

904904
//This should be replaced with a more appropriate chunking implementation
905-
vm.call_method(&raw, "write", vec![obj.clone()])
905+
vm.call_method(&raw, "write", vec![obj])
906906
}
907907

908908
fn buffered_writer_seekable(_self: PyObjectRef) -> bool {
@@ -981,7 +981,7 @@ fn text_io_wrapper_init(
981981
self_encoding.map_or_else(|| vm.get_none(), |s| vm.ctx.new_str(s)),
982982
)?;
983983
vm.set_attr(&instance, "errors", errors)?;
984-
vm.set_attr(&instance, "buffer", args.buffer.clone())?;
984+
vm.set_attr(&instance, "buffer", args.buffer)?;
985985

986986
Ok(())
987987
}
@@ -1027,7 +1027,7 @@ fn text_io_wrapper_read(
10271027
vm: &VirtualMachine,
10281028
) -> PyResult<String> {
10291029
let buffered_reader_class = vm.try_class("_io", "BufferedReader")?;
1030-
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
1030+
let raw = vm.get_attribute(instance, "buffer").unwrap();
10311031

10321032
if !objtype::isinstance(&raw, &buffered_reader_class) {
10331033
// TODO: this should be io.UnsupportedOperation error which derives both from ValueError *and* OSError
@@ -1058,7 +1058,7 @@ fn text_io_wrapper_write(
10581058
use std::str::from_utf8;
10591059

10601060
let buffered_writer_class = vm.try_class("_io", "BufferedWriter")?;
1061-
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
1061+
let raw = vm.get_attribute(instance, "buffer").unwrap();
10621062

10631063
if !objtype::isinstance(&raw, &buffered_writer_class) {
10641064
// TODO: this should be io.UnsupportedOperation error which derives from ValueError and OSError
@@ -1086,7 +1086,7 @@ fn text_io_wrapper_readline(
10861086
vm: &VirtualMachine,
10871087
) -> PyResult<String> {
10881088
let buffered_reader_class = vm.try_class("_io", "BufferedReader")?;
1089-
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
1089+
let raw = vm.get_attribute(instance, "buffer").unwrap();
10901090

10911091
if !objtype::isinstance(&raw, &buffered_reader_class) {
10921092
// TODO: this should be io.UnsupportedOperation error which derives both from ValueError *and* OSError
@@ -1242,7 +1242,7 @@ pub fn io_open(
12421242
let file_io_obj = vm.invoke(
12431243
&file_io_class,
12441244
PyFuncArgs::from((
1245-
Args::new(vec![file.clone(), vm.ctx.new_str(mode.clone())]),
1245+
Args::new(vec![file, vm.ctx.new_str(mode.clone())]),
12461246
KwArgs::new(maplit::hashmap! {
12471247
"closefd".to_owned() => vm.ctx.new_bool(opts.closefd),
12481248
"opener".to_owned() => opts.opener.unwrap_or_else(|| vm.get_none()),
@@ -1261,13 +1261,13 @@ pub fn io_open(
12611261
let buffered_writer_class = vm
12621262
.get_attribute(io_module.clone(), "BufferedWriter")
12631263
.unwrap();
1264-
vm.invoke(&buffered_writer_class, vec![file_io_obj.clone()])
1264+
vm.invoke(&buffered_writer_class, vec![file_io_obj])
12651265
}
12661266
'r' => {
12671267
let buffered_reader_class = vm
12681268
.get_attribute(io_module.clone(), "BufferedReader")
12691269
.unwrap();
1270-
vm.invoke(&buffered_reader_class, vec![file_io_obj.clone()])
1270+
vm.invoke(&buffered_reader_class, vec![file_io_obj])
12711271
}
12721272
//TODO: updating => PyBufferedRandom
12731273
_ => unimplemented!("'+' modes is not yet implemented"),

0 commit comments

Comments
 (0)