Skip to content
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,11 @@ inefficient_to_string = "warn"
redundant_clone = "warn"
debug_assert_with_mut_call = "warn"
unused_peekable = "warn"
manual_is_variant_and = "warn"
or_fun_call = "warn"
unnested_or_patterns = "warn"

# pedantic lints to enforce gradually
cloned_instead_of_copied = "warn"
manual_is_variant_and = "warn"
map_unwrap_or = "warn"
must_use_candidate = "warn"
11 changes: 3 additions & 8 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5412,6 +5412,7 @@ impl Compiler {
self.prepare_decorators(decorator_list)?;

let is_generic = type_params.is_some();
#[expect(clippy::map_unwrap_or, reason = "Changing this will not compile")]
let firstlineno = decorator_list
.first()
.map(|decorator| {
Expand Down Expand Up @@ -6882,19 +6883,13 @@ impl Compiler {
return Err(self.error(CodegenErrorType::MultipleStarArgs));
}
// star wildcard check
star_wildcard = pattern
.as_match_star()
.map(|m| m.name.is_none())
.unwrap_or(false);
star_wildcard = pattern.as_match_star().is_some_and(|m| m.name.is_none());
only_wildcard &= star_wildcard;
star = Some(i);
continue;
}
// wildcard check
only_wildcard &= pattern
.as_match_as()
.map(|m| m.name.is_none())
.unwrap_or(false);
only_wildcard &= pattern.as_match_as().is_some_and(|m| m.name.is_none());
}

// Keep the subject on top during the sequence and length checks.
Expand Down
26 changes: 9 additions & 17 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8907,8 +8907,7 @@ impl CodeInfo {
if let DeoptKind::ReturnIter { tail_start_idx } = deopt_kind {
let tail_instr_idx = real_instrs
.get(tail_start_idx)
.map(|(instr_idx, _)| *instr_idx)
.unwrap_or(block_instr_len);
.map_or(block_instr_len, |(instr_idx, _)| *instr_idx);
if !tail_returns_without_store(
&self.blocks,
&is_pre_handler,
Expand Down Expand Up @@ -9472,8 +9471,7 @@ impl CodeInfo {
block.disable_load_fast_borrow,
block
.start_depth
.map(|depth| depth.to_string())
.unwrap_or_else(|| String::from("None")),
.map_or_else(|| String::from("None"), |depth| depth.to_string()),
);
for info in &block.instructions {
let lineno = instruction_lineno(info);
Expand Down Expand Up @@ -10169,8 +10167,7 @@ fn mark_cold(blocks: &mut [Block]) {
let has_fallthrough = block
.instructions
.last()
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
.unwrap_or(true);
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump());
if has_fallthrough && block.next != BlockIdx::NULL {
let next_idx = block.next.idx();
if !blocks[next_idx].except_handler && !warm[next_idx] {
Expand Down Expand Up @@ -10209,11 +10206,9 @@ fn push_cold_blocks_to_end(blocks: &mut Vec<Block>) {
block.cold
&& block.next != BlockIdx::NULL
&& !blocks[block.next.idx()].cold
&& block
.instructions
.last()
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
.unwrap_or(true)
&& block.instructions.last().is_none_or(|ins| {
!ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump()
})
})
.map(|(idx, block)| (idx, block.next))
.collect();
Expand Down Expand Up @@ -13343,8 +13338,7 @@ fn duplicate_end_returns(blocks: &mut Vec<Block>, metadata: &CodeUnitMetadata) {
if current != last_block && !block.cold {
let last_ins = block.instructions.last();
let has_fallthrough = last_ins
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
.unwrap_or(true);
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump());
// Don't duplicate if block already ends with the same return pattern
let already_has_return = block.instructions.len() >= 2 && {
let n = block.instructions.len();
Expand Down Expand Up @@ -13518,8 +13512,7 @@ fn duplicate_named_except_cleanup_returns(blocks: &mut Vec<Block>, metadata: &Co
let fallthroughs_into_target = blocks[layout_pred.idx()]
.instructions
.last()
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
.unwrap_or(true);
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump());
if !fallthroughs_into_target || predecessors[target.idx()] < 2 {
continue;
}
Expand Down Expand Up @@ -13765,8 +13758,7 @@ pub(crate) fn label_exception_targets(blocks: &mut [Block]) {
let has_fallthrough = blocks[bi]
.instructions
.last()
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
.unwrap_or(true); // Empty block falls through
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump()); // Empty block falls through
if has_fallthrough {
visited[next.idx()] = true;
block_stacks[next.idx()] = Some(stack);
Expand Down
18 changes: 7 additions & 11 deletions crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,17 +1085,13 @@ impl SymbolTableBuilder {
}

fn enter_scope(&mut self, name: &str, typ: CompilerScope, line_number: u32) {
let is_nested = self
.tables
.last()
.map(|table| {
table.is_nested
|| matches!(
table.typ,
CompilerScope::Function | CompilerScope::AsyncFunction
)
})
.unwrap_or(false);
let is_nested = self.tables.last().is_some_and(|table| {
table.is_nested
|| matches!(
table.typ,
CompilerScope::Function | CompilerScope::AsyncFunction
)
});
// Inherit mangled_names from parent for non-class scopes
let inherited_mangled_names = self
.tables
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ where
let (index, c) = iter.next().ok_or_else(|| {
(
CFormatErrorType::IncompleteFormat,
iter.peek().map(|x| x.0).unwrap_or(0),
iter.peek().map_or(0, |x| x.0),
)
})?;
let format_type = match c.to_char_lossy() {
Expand Down
3 changes: 1 addition & 2 deletions crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ fn validate_base_field(item: &Item, base_path: &syn::Path) -> Result<Option<Toke
let base_name = base_path
.segments
.last()
.map(|s| s.ident.to_string())
.unwrap_or_else(|| quote!(#base_path).to_string());
.map_or_else(|| quote!(#base_path).to_string(), |s| s.ident.to_string());

match &item_struct.fields {
syn::Fields::Named(fields) => {
Expand Down
10 changes: 6 additions & 4 deletions crates/jit/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ fn extract_annotations_from_annotate_code(code: &CodeObject) -> HashMap<Wtf8Buf,
// Value can be a name (type ref) or a const string (forward ref)
let type_name = if val_is_const {
match code.constants.get(val_idx) {
Some(ConstantData::Str { value }) => value
.as_str()
.map(|s| s.to_owned())
.unwrap_or_else(|_| value.to_string_lossy().into_owned()),
Some(ConstantData::Str { value }) => {
value.as_str().map_or_else(
|_| value.to_string_lossy().into_owned(),
|s| s.to_owned(),
)
}
Some(other) => panic!(
"Unsupported annotation const for '{:?}' at idx {}: {:?}",
param_name, val_idx, other
Expand Down
49 changes: 14 additions & 35 deletions crates/sre_engine/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,29 +339,20 @@ const fn is_py_ascii_whitespace(b: u8) -> bool {

#[inline]
pub(crate) fn is_word(ch: u32) -> bool {
ch == '_' as u32
|| u8::try_from(ch)
.map(|x| x.is_ascii_alphanumeric())
.unwrap_or(false)
ch == '_' as u32 || u8::try_from(ch).is_ok_and(|x| x.is_ascii_alphanumeric())
}
#[inline]
pub(crate) fn is_space(ch: u32) -> bool {
u8::try_from(ch)
.map(is_py_ascii_whitespace)
.unwrap_or(false)
u8::try_from(ch).is_ok_and(is_py_ascii_whitespace)
}
#[inline]
pub(crate) fn is_digit(ch: u32) -> bool {
u8::try_from(ch)
.map(|x| x.is_ascii_digit())
.unwrap_or(false)
u8::try_from(ch).is_ok_and(|x| x.is_ascii_digit())
}
#[inline]
pub(crate) fn is_loc_alnum(ch: u32) -> bool {
// FIXME: Ignore the locales
u8::try_from(ch)
.map(|x| x.is_ascii_alphanumeric())
.unwrap_or(false)
u8::try_from(ch).is_ok_and(|x| x.is_ascii_alphanumeric())
}
#[inline]
pub(crate) fn is_loc_word(ch: u32) -> bool {
Expand All @@ -374,9 +365,7 @@ pub(crate) const fn is_linebreak(ch: u32) -> bool {
#[inline]
#[must_use]
pub fn lower_ascii(ch: u32) -> u32 {
u8::try_from(ch)
.map(|x| x.to_ascii_lowercase() as u32)
.unwrap_or(ch)
u8::try_from(ch).map_or(ch, |x| x.to_ascii_lowercase() as u32)
}
#[inline]
pub(crate) fn lower_locate(ch: u32) -> u32 {
Expand All @@ -386,16 +375,12 @@ pub(crate) fn lower_locate(ch: u32) -> u32 {
#[inline]
pub(crate) fn upper_locate(ch: u32) -> u32 {
// FIXME: Ignore the locales
u8::try_from(ch)
.map(|x| x.to_ascii_uppercase() as u32)
.unwrap_or(ch)
u8::try_from(ch).map_or(ch, |x| x.to_ascii_uppercase() as u32)
}
#[inline]
pub(crate) fn is_uni_digit(ch: u32) -> bool {
// TODO: check with cpython
char::try_from(ch)
.map(|x| x.is_ascii_digit())
.unwrap_or(false)
char::try_from(ch).is_ok_and(|x| x.is_ascii_digit())
}
#[inline]
pub(crate) fn is_uni_space(ch: u32) -> bool {
Expand Down Expand Up @@ -444,13 +429,11 @@ pub(crate) const fn is_uni_linebreak(ch: u32) -> bool {
#[inline]
pub(crate) fn is_uni_alnum(ch: u32) -> bool {
// TODO: check with cpython
char::try_from(ch)
.map(|c| {
GeneralCategoryGroup::Letter
.union(GeneralCategoryGroup::Number)
.contains(GeneralCategory::for_char(c))
})
.unwrap_or(false)
char::try_from(ch).is_ok_and(|c| {
GeneralCategoryGroup::Letter
.union(GeneralCategoryGroup::Number)
.contains(GeneralCategory::for_char(c))
})
}
#[inline]
pub(crate) fn is_uni_word(ch: u32) -> bool {
Expand All @@ -460,15 +443,11 @@ pub(crate) fn is_uni_word(ch: u32) -> bool {
#[must_use]
pub fn lower_unicode(ch: u32) -> u32 {
// TODO: check with cpython
char::try_from(ch)
.map(|x| x.to_lowercase().next().unwrap() as u32)
.unwrap_or(ch)
char::try_from(ch).map_or(ch, |x| x.to_lowercase().next().unwrap() as u32)
}
#[inline]
#[must_use]
pub fn upper_unicode(ch: u32) -> u32 {
// TODO: check with cpython
char::try_from(ch)
.map(|x| x.to_uppercase().next().unwrap() as u32)
.unwrap_or(ch)
char::try_from(ch).map_or(ch, |x| x.to_uppercase().next().unwrap() as u32)
}
3 changes: 1 addition & 2 deletions crates/stdlib/src/_asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,7 @@ pub(crate) mod _asyncio {
{
let s = state
.str(vm)
.map(|s| s.as_wtf8().to_lowercase())
.unwrap_or_else(|_| Wtf8Buf::from("unknown"));
.map_or_else(|_| Wtf8Buf::from("unknown"), |s| s.as_wtf8().to_lowercase());
return Ok(s);
}
Ok(Wtf8Buf::from("state=unknown"))
Expand Down
57 changes: 21 additions & 36 deletions crates/stdlib/src/_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,22 @@ mod _opcode {

#[pyfunction]
fn stack_effect(args: StackEffectArgs, vm: &VirtualMachine) -> PyResult<i32> {
let oparg = args
.oparg
.map(|v| {
if !v.fast_isinstance(vm.ctx.types.int_type) {
return Err(vm.new_type_error(format!(
let oparg = args.oparg.map_or(Ok(0), |v| {
if !v.fast_isinstance(vm.ctx.types.int_type) {
return Err(vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
v.class().name()
)));
}
v.downcast_ref::<PyInt>()
.ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
v.class().name()
)));
}
v.downcast_ref::<PyInt>()
.ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
v.class().name()
))
})?
.try_to_primitive::<u32>(vm)
})
.unwrap_or(Ok(0))?;
))
})?
.try_to_primitive::<u32>(vm)
})?;

let jump: Option<bool> = match args.jump {
Some(v) => {
Expand Down Expand Up @@ -99,49 +96,37 @@ mod _opcode {

#[pyfunction]
fn has_arg(opcode: i32) -> bool {
try_from_i32(opcode).map(|op| op.has_arg()).unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.has_arg())
}

#[pyfunction]
fn has_const(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.has_const())
.unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.has_const())
}

#[pyfunction]
fn has_name(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.has_name())
.unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.has_name())
}

#[pyfunction]
fn has_jump(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.has_jump())
.unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.has_jump())
}

#[pyfunction]
fn has_free(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.has_free())
.unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.has_free())
}

#[pyfunction]
fn has_local(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.has_local())
.unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.has_local())
}

#[pyfunction]
fn has_exc(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.is_block_push())
.unwrap_or(false)
try_from_i32(opcode).is_ok_and(|op| op.is_block_push())
}

#[pyfunction]
Expand Down
Loading
Loading