Skip to content

Remove redundent to_owned() and to_string() calls #5836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 6 additions & 12 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,11 @@ mod array {
vm: &VirtualMachine,
) -> PyResult {
let spec = spec.as_str().chars().exactly_one().map_err(|_| {
vm.new_type_error(
"array() argument 1 must be a unicode character, not str".to_owned(),
)
vm.new_type_error("array() argument 1 must be a unicode character, not str")
})?;

if cls.is(PyArray::class(&vm.ctx)) && !kwargs.is_empty() {
return Err(
vm.new_type_error("array.array() takes no keyword arguments".to_owned())
);
return Err(vm.new_type_error("array.array() takes no keyword arguments"));
}

let mut array =
Expand Down Expand Up @@ -856,9 +852,7 @@ mod array {

fn _from_bytes(&self, b: &[u8], itemsize: usize, vm: &VirtualMachine) -> PyResult<()> {
if b.len() % itemsize != 0 {
return Err(
vm.new_value_error("bytes length not a multiple of item size".to_owned())
);
return Err(vm.new_value_error("bytes length not a multiple of item size"));
}
if b.len() / itemsize > 0 {
self.try_resizable(vm)?.frombytes(b);
Expand All @@ -877,15 +871,15 @@ mod array {
fn fromfile(&self, f: PyObjectRef, n: isize, vm: &VirtualMachine) -> PyResult<()> {
let itemsize = self.itemsize();
if n < 0 {
return Err(vm.new_value_error("negative count".to_owned()));
return Err(vm.new_value_error("negative count"));
}
let n = vm.check_repeat_or_overflow_error(itemsize, n)?;
let n_bytes = n * itemsize;

let b = vm.call_method(&f, "read", (n_bytes,))?;
let b = b
.downcast::<PyBytes>()
.map_err(|_| vm.new_type_error("read() didn't return bytes".to_owned()))?;
.map_err(|_| vm.new_type_error("read() didn't return bytes"))?;

let not_enough_bytes = b.len() != n_bytes;

Expand Down Expand Up @@ -927,7 +921,7 @@ mod array {
fn pop(zelf: &Py<Self>, i: OptionalArg<isize>, vm: &VirtualMachine) -> PyResult {
let mut w = zelf.try_resizable(vm)?;
if w.len() == 0 {
Err(vm.new_index_error("pop from empty array".to_owned()))
Err(vm.new_index_error("pop from empty array"))
} else {
w.pop(i.unwrap_or(-1), vm)
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ mod decl {
let sep_char = if let OptionalArg::Present(sep_buf) = sep {
sep_buf.with_ref(|sep_bytes| {
if sep_bytes.len() != 1 {
return Err(vm.new_value_error("sep must be length 1.".to_owned()));
return Err(vm.new_value_error("sep must be length 1."));
}
let sep_char = sep_bytes[0];
if !sep_char.is_ascii() {
return Err(vm.new_value_error("sep must be ASCII.".to_owned()));
return Err(vm.new_value_error("sep must be ASCII."));
}
Ok(Some(sep_char))
})?
Expand Down
3 changes: 1 addition & 2 deletions stdlib/src/bisect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ mod _bisect {
// Default is always a Some so we can safely unwrap.
let lo = handle_default(lo, vm)?
.map(|value| {
usize::try_from(value)
.map_err(|_| vm.new_value_error("lo must be non-negative".to_owned()))
usize::try_from(value).map_err(|_| vm.new_value_error("lo must be non-negative"))
})
.unwrap_or(Ok(0))?;
let hi = handle_default(hi, vm)?
Expand Down
12 changes: 5 additions & 7 deletions stdlib/src/bz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod _bz2 {

#[pymethod(name = "__reduce__")]
fn reduce(&self, vm: &VirtualMachine) -> PyResult<()> {
Err(vm.new_type_error("cannot pickle '_bz2.BZ2Decompressor' object".to_owned()))
Err(vm.new_type_error("cannot pickle '_bz2.BZ2Decompressor' object"))
}

// TODO: mro()?
Expand Down Expand Up @@ -140,9 +140,7 @@ mod _bz2 {
let level = match compresslevel {
valid_level @ 1..=9 => bzip2::Compression::new(valid_level as u32),
_ => {
return Err(
vm.new_value_error("compresslevel must be between 1 and 9".to_owned())
);
return Err(vm.new_value_error("compresslevel must be between 1 and 9"));
}
};

Expand All @@ -164,7 +162,7 @@ mod _bz2 {
fn compress(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
let mut state = self.state.lock();
if state.flushed {
return Err(vm.new_value_error("Compressor has been flushed".to_owned()));
return Err(vm.new_value_error("Compressor has been flushed"));
}

// let CompressorState { flushed, encoder } = &mut *state;
Expand All @@ -179,7 +177,7 @@ mod _bz2 {
fn flush(&self, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
let mut state = self.state.lock();
if state.flushed {
return Err(vm.new_value_error("Repeated call to flush()".to_owned()));
return Err(vm.new_value_error("Repeated call to flush()"));
}

// let CompressorState { flushed, encoder } = &mut *state;
Expand All @@ -193,7 +191,7 @@ mod _bz2 {

#[pymethod(name = "__reduce__")]
fn reduce(&self, vm: &VirtualMachine) -> PyResult<()> {
Err(vm.new_type_error("cannot pickle '_bz2.BZ2Compressor' object".to_owned()))
Err(vm.new_type_error("cannot pickle '_bz2.BZ2Compressor' object"))
}
}
}
4 changes: 2 additions & 2 deletions stdlib/src/cmath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod cmath {
let abs_tol = args.abs_tol.map_or(0.0, Into::into);

if rel_tol < 0.0 || abs_tol < 0.0 {
return Err(vm.new_value_error("tolerances must be non-negative".to_owned()));
return Err(vm.new_value_error("tolerances must be non-negative"));
}

if a == b {
Expand Down Expand Up @@ -201,7 +201,7 @@ mod cmath {
if !result.is_finite() && value.is_finite() {
// CPython doesn't return `inf` when called with finite
// values, it raises OverflowError instead.
Err(vm.new_overflow_error("math range error".to_owned()))
Err(vm.new_overflow_error("math range error"))
} else {
Ok(result)
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,6 @@ pub struct EofError;

impl ToPyException for EofError {
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
vm.new_eof_error("End of stream already reached".to_owned())
vm.new_eof_error("End of stream already reached")
}
}
2 changes: 1 addition & 1 deletion stdlib/src/contextvars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ mod _contextvars {
type Args = FuncArgs;

fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_runtime_error("Tokens can only be created by ContextVars".to_owned()))
Err(vm.new_runtime_error("Tokens can only be created by ContextVars"))
}
fn py_new(_cls: PyTypeRef, _args: Self::Args, _vm: &VirtualMachine) -> PyResult {
unreachable!()
Expand Down
18 changes: 8 additions & 10 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ mod _csv {
vm: &VirtualMachine,
) -> PyResult<()> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
return Err(vm.new_type_error("argument 0 must be a string".to_string()));
return Err(vm.new_type_error("argument 0 must be a string"));
};
let dialect = match dialect {
OptionalArg::Present(d) => PyDialect::try_from_object(vm, d)
.map_err(|_| vm.new_type_error("argument 1 must be a dialect object".to_owned()))?,
.map_err(|_| vm.new_type_error("argument 1 must be a dialect object"))?,
OptionalArg::Missing => opts.result(vm)?,
};
let dialect = opts.update_py_dialect(dialect);
Expand Down Expand Up @@ -328,7 +328,7 @@ mod _csv {
vm: &VirtualMachine,
) -> PyResult<rustpython_vm::builtins::PyListRef> {
if !rest.args.is_empty() || !rest.kwargs.is_empty() {
return Err(vm.new_type_error("too many argument".to_string()));
return Err(vm.new_type_error("too many argument"));
}
let g = GLOBAL_HASHMAP.lock();
let t = g
Expand All @@ -351,7 +351,7 @@ mod _csv {
)));
}
let Ok(new_size) = rest.args.first().unwrap().try_int(vm) else {
return Err(vm.new_type_error("limit must be an integer".to_string()));
return Err(vm.new_type_error("limit must be an integer"));
};
*GLOBAL_FIELD_LIMIT.lock() = new_size.try_to_primitive::<isize>(vm)?;
}
Expand Down Expand Up @@ -392,7 +392,7 @@ mod _csv {
Some(write_meth) => write_meth,
None if file.is_callable() => file,
None => {
return Err(vm.new_type_error("argument 1 must have a \"write\" method".to_owned()));
return Err(vm.new_type_error("argument 1 must have a \"write\" method"));
}
};

Expand Down Expand Up @@ -438,9 +438,7 @@ mod _csv {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let num = obj.try_int(vm)?.try_to_primitive::<isize>(vm)?;
num.try_into().map_err(|_| {
vm.new_value_error(
"can not convert to QuoteStyle enum from input argument".to_string(),
)
vm.new_value_error("can not convert to QuoteStyle enum from input argument")
})
}
}
Expand Down Expand Up @@ -1016,7 +1014,7 @@ mod _csv {
prev_end = end;
let s = std::str::from_utf8(&buffer[range.clone()])
// not sure if this is possible - the input was all strings
.map_err(|_e| vm.new_unicode_decode_error("csv not utf8".to_owned()))?;
.map_err(|_e| vm.new_unicode_decode_error("csv not utf8"))?;
// Rustpython TODO!
// Incomplete implementation
if let QuoteStyle::Nonnumeric = zelf.dialect.quoting {
Expand Down Expand Up @@ -1125,7 +1123,7 @@ mod _csv {
handle_res!(writer.terminator(&mut buffer[buffer_offset..]));
}
let s = std::str::from_utf8(&buffer[..buffer_offset])
.map_err(|_| vm.new_unicode_decode_error("csv not utf8".to_owned()))?;
.map_err(|_| vm.new_unicode_decode_error("csv not utf8"))?;

self.write.call((s,), vm)
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ mod decl {
#[cfg(not(feature = "compiler"))]
{
let _ = co_str;
return Err(vm.new_runtime_error(
"dis.dis() with str argument requires `compiler` feature".to_owned(),
));
return Err(
vm.new_runtime_error("dis.dis() with str argument requires `compiler` feature")
);
}
#[cfg(feature = "compiler")]
{
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod fcntl {
let s = arg.borrow_bytes();
arg_len = s.len();
buf.get_mut(..arg_len)
.ok_or_else(|| vm.new_value_error("fcntl string arg too long".to_owned()))?
.ok_or_else(|| vm.new_value_error("fcntl string arg too long"))?
.copy_from_slice(&s)
}
let ret = unsafe { libc::fcntl(fd, cmd, buf.as_mut_ptr()) };
Expand Down Expand Up @@ -104,7 +104,7 @@ mod fcntl {
let mut buf = [0u8; BUF_SIZE + 1]; // nul byte
let mut fill_buf = |b: &[u8]| {
if b.len() > BUF_SIZE {
return Err(vm.new_value_error("fcntl string arg too long".to_owned()));
return Err(vm.new_value_error("fcntl string arg too long"));
}
buf[..b.len()].copy_from_slice(b);
Ok(b.len())
Expand Down Expand Up @@ -181,7 +181,7 @@ mod fcntl {
} else if (cmd & libc::LOCK_EX) != 0 {
try_into_l_type!(libc::F_WRLCK)
} else {
return Err(vm.new_value_error("unrecognized lockf argument".to_owned()));
return Err(vm.new_value_error("unrecognized lockf argument"));
}?;
l.l_start = match start {
OptionalArg::Present(s) => s.try_to_primitive(vm)?,
Expand Down
24 changes: 12 additions & 12 deletions stdlib/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,61 @@

#[pyfunction]
fn enable(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn disable(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_count(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_debug(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_objects(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_refererts(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {

Check warning on line 43 in stdlib/src/gc.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (refererts)
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_referrers(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_stats(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn get_threshold(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn is_tracked(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn set_debug(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}

#[pyfunction]
fn set_threshold(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_not_implemented_error("".to_owned()))
Err(vm.new_not_implemented_error(""))
}
}
2 changes: 1 addition & 1 deletion stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub mod _hashlib {
impl XofDigestArgs {
fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
usize::try_from(self.length)
.map_err(|_| vm.new_value_error("length must be non-negative".to_owned()))
.map_err(|_| vm.new_value_error("length must be non-negative"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mod _json {
type Args = (PyStrRef, isize);
fn call(zelf: &Py<Self>, (pystr, idx): Self::Args, vm: &VirtualMachine) -> PyResult {
if idx < 0 {
return Err(vm.new_value_error("idx cannot be negative".to_owned()));
return Err(vm.new_value_error("idx cannot be negative"));
}
let idx = idx as usize;
let mut chars = pystr.as_str().chars();
Expand Down
8 changes: 3 additions & 5 deletions stdlib/src/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ mod _lzma {

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
if args.format == FORMAT_RAW && args.memlimit.is_some() {
return Err(
vm.new_value_error("Cannot specify memory limit with FORMAT_RAW".to_string())
);
return Err(vm.new_value_error("Cannot specify memory limit with FORMAT_RAW"));
}
let memlimit = args.memlimit.unwrap_or(u64::MAX);
let filters = args.filters.unwrap_or(0);
Expand Down Expand Up @@ -316,8 +314,8 @@ mod _lzma {
filters: Option<Vec<PyObjectRef>>,
vm: &VirtualMachine,
) -> PyResult<Stream> {
let real_check = int_to_check(check)
.ok_or_else(|| vm.new_type_error("Invalid check value".to_string()))?;
let real_check =
int_to_check(check).ok_or_else(|| vm.new_type_error("Invalid check value"))?;
if let Some(filters) = filters {
let filters = parse_filter_chain_spec(filters, vm)?;
Ok(Stream::new_stream_encoder(&filters, real_check)
Expand Down
Loading
Loading