Skip to content

Commit 12c96d0

Browse files
committed
Remove useless and_then
1 parent ad29fbd commit 12c96d0

File tree

12 files changed

+92
-110
lines changed

12 files changed

+92
-110
lines changed

compiler/codegen/src/symboltable.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,20 @@ impl SymbolTableAnalyzer {
414414
st_typ: SymbolTableType,
415415
) -> Option<SymbolScope> {
416416
sub_tables.iter().find_map(|st| {
417-
st.symbols.get(name).and_then(|sym| {
418-
if sym.scope == SymbolScope::Free || sym.flags.contains(SymbolFlags::FREE_CLASS) {
419-
if st_typ == SymbolTableType::Class && name != "__class__" {
420-
None
421-
} else {
422-
Some(SymbolScope::Cell)
423-
}
424-
} else if sym.scope == SymbolScope::GlobalExplicit && self.tables.is_empty() {
425-
// the symbol is defined on the module level, and an inner scope declares
426-
// a global that points to it
427-
Some(SymbolScope::GlobalExplicit)
428-
} else {
417+
let sym = st.symbols.get(name)?;
418+
if sym.scope == SymbolScope::Free || sym.flags.contains(SymbolFlags::FREE_CLASS) {
419+
if st_typ == SymbolTableType::Class && name != "__class__" {
429420
None
421+
} else {
422+
Some(SymbolScope::Cell)
430423
}
431-
})
424+
} else if sym.scope == SymbolScope::GlobalExplicit && self.tables.is_empty() {
425+
// the symbol is defined on the module level, and an inner scope declares
426+
// a global that points to it
427+
Some(SymbolScope::GlobalExplicit)
428+
} else {
429+
None
430+
}
432431
})
433432
}
434433

stdlib/src/sqlite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ mod _sqlite {
809809
type Args = ConnectArgs;
810810

811811
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
812-
Self::new(args, vm).and_then(|x| x.into_ref_with_type(vm, cls).map(Into::into))
812+
Ok(Self::new(args, vm)?.into_ref_with_type(vm, cls)?.into())
813813
}
814814
}
815815

stdlib/src/zlib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,14 @@ mod zlib {
248248
} = args;
249249
data.with_ref(|data| {
250250
let mut d = InitOptions::new(wbits.value, vm)?.decompress();
251-
252-
_decompress(data, &mut d, bufsize.value, None, false, vm).and_then(
253-
|(buf, stream_end)| {
254-
if stream_end {
255-
Ok(buf)
256-
} else {
257-
Err(new_zlib_error(
258-
"Error -5 while decompressing data: incomplete or truncated stream",
259-
vm,
260-
))
261-
}
262-
},
263-
)
251+
let (buf, stream_end) = _decompress(data, &mut d, bufsize.value, None, false, vm)?;
252+
if !stream_end {
253+
return Err(new_zlib_error(
254+
"Error -5 while decompressing data: incomplete or truncated stream",
255+
vm,
256+
));
257+
}
258+
Ok(buf)
264259
})
265260
}
266261

vm/src/builtins/builtin_func.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ impl PyBuiltinFunction {
147147
}
148148
#[pygetset(magic)]
149149
fn text_signature(&self) -> Option<String> {
150-
self.value.doc.as_ref().and_then(|doc| {
151-
type_::get_text_signature_from_internal_doc(self.value.name.as_str(), doc.as_str())
152-
.map(|signature| signature.to_string())
153-
})
150+
let doc = self.value.doc.as_ref()?;
151+
let signature =
152+
type_::get_text_signature_from_internal_doc(self.value.name.as_str(), doc.as_str())?;
153+
Some(signature.to_owned())
154154
}
155155
}
156156

vm/src/builtins/module.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ impl PyModule {
6161
}
6262

6363
fn name(zelf: PyRef<Self>, vm: &VirtualMachine) -> Option<PyStrRef> {
64-
zelf.as_object()
64+
let name = zelf
65+
.as_object()
6566
.generic_getattr_opt(identifier!(vm, __name__).to_owned(), None, vm)
66-
.unwrap_or(None)
67-
.and_then(|obj| obj.downcast::<PyStr>().ok())
67+
.unwrap_or_default()?;
68+
name.downcast::<PyStr>().ok()
6869
}
6970

7071
#[pymethod(magic)]
@@ -145,10 +146,9 @@ impl Representable for PyModule {
145146
fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
146147
let importlib = vm.import("_frozen_importlib", None, 0)?;
147148
let module_repr = importlib.get_attr("_module_repr", vm)?;
148-
module_repr.call((zelf.to_owned(),), vm).and_then(|obj| {
149-
obj.downcast()
150-
.map_err(|_| vm.new_type_error("_module_repr did not return a string".into()))
151-
})
149+
let repr = module_repr.call((zelf.to_owned(),), vm)?;
150+
repr.downcast()
151+
.map_err(|_| vm.new_type_error("_module_repr did not return a string".into()))
152152
}
153153

154154
#[cold]

vm/src/import.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,24 @@ pub fn make_frozen(vm: &VirtualMachine, name: &str) -> PyResult<PyRef<PyCode>> {
8181
}
8282

8383
pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
84-
make_frozen(vm, module_name).and_then(|frozen| {
85-
let module = import_codeobj(vm, module_name, frozen, false)?;
86-
// TODO: give a correct origname here
87-
module.set_attr("__origname__", vm.ctx.new_str(module_name.to_owned()), vm)?;
88-
Ok(module)
89-
})
84+
let frozen = make_frozen(vm, module_name)?;
85+
let module = import_codeobj(vm, module_name, frozen, false)?;
86+
// TODO: give a correct origname here
87+
module.set_attr("__origname__", vm.ctx.new_str(module_name.to_owned()), vm)?;
88+
Ok(module)
9089
}
9190

9291
pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
93-
vm.state
94-
.module_inits
95-
.get(module_name)
96-
.ok_or_else(|| {
97-
vm.new_import_error(
98-
format!("Cannot import builtin module {module_name}"),
99-
module_name,
100-
)
101-
})
102-
.and_then(|make_module_func| {
103-
let module = make_module_func(vm);
104-
let sys_modules = vm.sys_module.get_attr("modules", vm)?;
105-
sys_modules.set_item(module_name, module.clone(), vm)?;
106-
Ok(module)
107-
})
92+
let make_module_func = vm.state.module_inits.get(module_name).ok_or_else(|| {
93+
vm.new_import_error(
94+
format!("Cannot import builtin module {module_name}"),
95+
module_name,
96+
)
97+
})?;
98+
let module = make_module_func(vm);
99+
let sys_modules = vm.sys_module.get_attr("modules", vm)?;
100+
sys_modules.set_item(module_name, module.clone(), vm)?;
101+
Ok(module)
108102
}
109103

110104
#[cfg(feature = "rustpython-compiler")]

vm/src/protocol/iter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl ToPyResult for PyIterReturn {
197197

198198
impl ToPyResult for PyResult<PyIterReturn> {
199199
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
200-
self.and_then(|obj| obj.to_pyresult(vm))
200+
self?.to_pyresult(vm)
201201
}
202202
}
203203

@@ -234,11 +234,11 @@ where
234234
type Item = PyResult<T>;
235235

236236
fn next(&mut self) -> Option<Self::Item> {
237-
PyIter::new(self.obj.borrow())
237+
let obj = PyIter::new(self.obj.borrow())
238238
.next(self.vm)
239239
.map(|iret| iret.into_result().ok())
240-
.transpose()
241-
.map(|x| x.and_then(|obj| T::try_from_object(self.vm, obj)))
240+
.transpose()?;
241+
Some(obj.and_then(|obj| T::try_from_object(self.vm, obj)))
242242
}
243243

244244
#[inline]

vm/src/protocol/object.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -461,37 +461,36 @@ impl PyObject {
461461
}
462462

463463
fn abstract_isinstance(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
464-
if let Ok(typ) = PyTypeRef::try_from_object(vm, cls.to_owned()) {
464+
let r = if let Ok(typ) = PyTypeRef::try_from_object(vm, cls.to_owned()) {
465465
if self.class().fast_issubclass(&typ) {
466-
Ok(true)
466+
true
467467
} else if let Ok(icls) = PyTypeRef::try_from_object(
468468
vm,
469469
self.to_owned().get_attr(identifier!(vm, __class__), vm)?,
470470
) {
471471
if icls.is(self.class()) {
472-
Ok(false)
472+
false
473473
} else {
474-
Ok(icls.fast_issubclass(&typ))
474+
icls.fast_issubclass(&typ)
475475
}
476476
} else {
477-
Ok(false)
477+
false
478478
}
479479
} else {
480480
self.check_cls(cls, vm, || {
481481
format!(
482482
"isinstance() arg 2 must be a type or tuple of types, not {}",
483483
cls.class()
484484
)
485-
})
486-
.and_then(|_| {
487-
let icls: PyObjectRef = self.to_owned().get_attr(identifier!(vm, __class__), vm)?;
488-
if vm.is_none(&icls) {
489-
Ok(false)
490-
} else {
491-
icls.abstract_issubclass(cls, vm)
492-
}
493-
})
494-
}
485+
})?;
486+
let icls: PyObjectRef = self.to_owned().get_attr(identifier!(vm, __class__), vm)?;
487+
if vm.is_none(&icls) {
488+
false
489+
} else {
490+
icls.abstract_issubclass(cls, vm)?
491+
}
492+
};
493+
Ok(r)
495494
}
496495

497496
/// Determines if `self` is an instance of `cls`, either directly, indirectly or virtually via

vm/src/stdlib/builtins.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,12 @@ mod builtins {
8484

8585
#[pyfunction]
8686
fn chr(i: PyIntRef, vm: &VirtualMachine) -> PyResult<String> {
87-
match i
87+
let value = i
8888
.try_to_primitive::<isize>(vm)?
8989
.to_u32()
9090
.and_then(char::from_u32)
91-
{
92-
Some(value) => Ok(value.to_string()),
93-
None => Err(vm.new_value_error("chr() arg not in range(0x110000)".to_owned())),
94-
}
91+
.ok_or_else(|| vm.new_value_error("chr() arg not in range(0x110000)".to_owned()))?;
92+
Ok(value.to_string())
9593
}
9694

9795
#[derive(FromArgs)]

vm/src/stdlib/io.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ mod _io {
175175
impl OptionalSize {
176176
#[allow(clippy::wrong_self_convention)]
177177
pub fn to_usize(self) -> Option<usize> {
178-
self.size.and_then(|v| v.to_usize())
178+
self.size?.to_usize()
179179
}
180180

181181
pub fn try_usize(self, vm: &VirtualMachine) -> PyResult<Option<usize>> {
@@ -2225,11 +2225,11 @@ mod _io {
22252225
codec.get_incremental_encoder(Some(errors.clone()), vm)?;
22262226
let encoding_name = vm.get_attribute_opt(incremental_encoder.clone(), "name")?;
22272227
let encodefunc = encoding_name.and_then(|name| {
2228-
name.payload::<PyStr>()
2229-
.and_then(|name| match name.as_str() {
2230-
"utf-8" => Some(textio_encode_utf8 as EncodeFunc),
2231-
_ => None,
2232-
})
2228+
let name = name.payload::<PyStr>()?;
2229+
match name.as_str() {
2230+
"utf-8" => Some(textio_encode_utf8 as EncodeFunc),
2231+
_ => None,
2232+
}
22332233
});
22342234
Some((incremental_encoder, encodefunc))
22352235
} else {

0 commit comments

Comments
 (0)