Skip to content

Commit 4a46e84

Browse files
authored
Add map_unwrap_or clippy rule (#7829)
1 parent 949a620 commit 4a46e84

50 files changed

Lines changed: 208 additions & 339 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,11 @@ inefficient_to_string = "warn"
338338
redundant_clone = "warn"
339339
debug_assert_with_mut_call = "warn"
340340
unused_peekable = "warn"
341-
manual_is_variant_and = "warn"
342341
or_fun_call = "warn"
343342
unnested_or_patterns = "warn"
344343

345344
# pedantic lints to enforce gradually
346345
cloned_instead_of_copied = "warn"
346+
manual_is_variant_and = "warn"
347+
map_unwrap_or = "warn"
347348
must_use_candidate = "warn"

crates/codegen/src/compile.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5412,6 +5412,7 @@ impl Compiler {
54125412
self.prepare_decorators(decorator_list)?;
54135413

54145414
let is_generic = type_params.is_some();
5415+
#[expect(clippy::map_unwrap_or, reason = "Changing this will not compile")]
54155416
let firstlineno = decorator_list
54165417
.first()
54175418
.map(|decorator| {
@@ -6882,19 +6883,13 @@ impl Compiler {
68826883
return Err(self.error(CodegenErrorType::MultipleStarArgs));
68836884
}
68846885
// star wildcard check
6885-
star_wildcard = pattern
6886-
.as_match_star()
6887-
.map(|m| m.name.is_none())
6888-
.unwrap_or(false);
6886+
star_wildcard = pattern.as_match_star().is_some_and(|m| m.name.is_none());
68896887
only_wildcard &= star_wildcard;
68906888
star = Some(i);
68916889
continue;
68926890
}
68936891
// wildcard check
6894-
only_wildcard &= pattern
6895-
.as_match_as()
6896-
.map(|m| m.name.is_none())
6897-
.unwrap_or(false);
6892+
only_wildcard &= pattern.as_match_as().is_some_and(|m| m.name.is_none());
68986893
}
68996894

69006895
// Keep the subject on top during the sequence and length checks.

crates/codegen/src/ir.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8907,8 +8907,7 @@ impl CodeInfo {
89078907
if let DeoptKind::ReturnIter { tail_start_idx } = deopt_kind {
89088908
let tail_instr_idx = real_instrs
89098909
.get(tail_start_idx)
8910-
.map(|(instr_idx, _)| *instr_idx)
8911-
.unwrap_or(block_instr_len);
8910+
.map_or(block_instr_len, |(instr_idx, _)| *instr_idx);
89128911
if !tail_returns_without_store(
89138912
&self.blocks,
89148913
&is_pre_handler,
@@ -9472,8 +9471,7 @@ impl CodeInfo {
94729471
block.disable_load_fast_borrow,
94739472
block
94749473
.start_depth
9475-
.map(|depth| depth.to_string())
9476-
.unwrap_or_else(|| String::from("None")),
9474+
.map_or_else(|| String::from("None"), |depth| depth.to_string()),
94779475
);
94789476
for info in &block.instructions {
94799477
let lineno = instruction_lineno(info);
@@ -10169,8 +10167,7 @@ fn mark_cold(blocks: &mut [Block]) {
1016910167
let has_fallthrough = block
1017010168
.instructions
1017110169
.last()
10172-
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
10173-
.unwrap_or(true);
10170+
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump());
1017410171
if has_fallthrough && block.next != BlockIdx::NULL {
1017510172
let next_idx = block.next.idx();
1017610173
if !blocks[next_idx].except_handler && !warm[next_idx] {
@@ -10209,11 +10206,9 @@ fn push_cold_blocks_to_end(blocks: &mut Vec<Block>) {
1020910206
block.cold
1021010207
&& block.next != BlockIdx::NULL
1021110208
&& !blocks[block.next.idx()].cold
10212-
&& block
10213-
.instructions
10214-
.last()
10215-
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
10216-
.unwrap_or(true)
10209+
&& block.instructions.last().is_none_or(|ins| {
10210+
!ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump()
10211+
})
1021710212
})
1021810213
.map(|(idx, block)| (idx, block.next))
1021910214
.collect();
@@ -13343,8 +13338,7 @@ fn duplicate_end_returns(blocks: &mut Vec<Block>, metadata: &CodeUnitMetadata) {
1334313338
if current != last_block && !block.cold {
1334413339
let last_ins = block.instructions.last();
1334513340
let has_fallthrough = last_ins
13346-
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
13347-
.unwrap_or(true);
13341+
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump());
1334813342
// Don't duplicate if block already ends with the same return pattern
1334913343
let already_has_return = block.instructions.len() >= 2 && {
1335013344
let n = block.instructions.len();
@@ -13518,8 +13512,7 @@ fn duplicate_named_except_cleanup_returns(blocks: &mut Vec<Block>, metadata: &Co
1351813512
let fallthroughs_into_target = blocks[layout_pred.idx()]
1351913513
.instructions
1352013514
.last()
13521-
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
13522-
.unwrap_or(true);
13515+
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump());
1352313516
if !fallthroughs_into_target || predecessors[target.idx()] < 2 {
1352413517
continue;
1352513518
}
@@ -13765,8 +13758,7 @@ pub(crate) fn label_exception_targets(blocks: &mut [Block]) {
1376513758
let has_fallthrough = blocks[bi]
1376613759
.instructions
1376713760
.last()
13768-
.map(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump())
13769-
.unwrap_or(true); // Empty block falls through
13761+
.is_none_or(|ins| !ins.instr.is_scope_exit() && !ins.instr.is_unconditional_jump()); // Empty block falls through
1377013762
if has_fallthrough {
1377113763
visited[next.idx()] = true;
1377213764
block_stacks[next.idx()] = Some(stack);

crates/codegen/src/symboltable.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,17 +1085,13 @@ impl SymbolTableBuilder {
10851085
}
10861086

10871087
fn enter_scope(&mut self, name: &str, typ: CompilerScope, line_number: u32) {
1088-
let is_nested = self
1089-
.tables
1090-
.last()
1091-
.map(|table| {
1092-
table.is_nested
1093-
|| matches!(
1094-
table.typ,
1095-
CompilerScope::Function | CompilerScope::AsyncFunction
1096-
)
1097-
})
1098-
.unwrap_or(false);
1088+
let is_nested = self.tables.last().is_some_and(|table| {
1089+
table.is_nested
1090+
|| matches!(
1091+
table.typ,
1092+
CompilerScope::Function | CompilerScope::AsyncFunction
1093+
)
1094+
});
10991095
// Inherit mangled_names from parent for non-class scopes
11001096
let inherited_mangled_names = self
11011097
.tables

crates/common/src/cformat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ where
620620
let (index, c) = iter.next().ok_or_else(|| {
621621
(
622622
CFormatErrorType::IncompleteFormat,
623-
iter.peek().map(|x| x.0).unwrap_or(0),
623+
iter.peek().map_or(0, |x| x.0),
624624
)
625625
})?;
626626
let format_type = match c.to_char_lossy() {

crates/derive-impl/src/pyclass.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ fn validate_base_field(item: &Item, base_path: &syn::Path) -> Result<Option<Toke
331331
let base_name = base_path
332332
.segments
333333
.last()
334-
.map(|s| s.ident.to_string())
335-
.unwrap_or_else(|| quote!(#base_path).to_string());
334+
.map_or_else(|| quote!(#base_path).to_string(), |s| s.ident.to_string());
336335

337336
match &item_struct.fields {
338337
syn::Fields::Named(fields) => {

crates/jit/tests/common.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ fn extract_annotations_from_annotate_code(code: &CodeObject) -> HashMap<Wtf8Buf,
116116
// Value can be a name (type ref) or a const string (forward ref)
117117
let type_name = if val_is_const {
118118
match code.constants.get(val_idx) {
119-
Some(ConstantData::Str { value }) => value
120-
.as_str()
121-
.map(|s| s.to_owned())
122-
.unwrap_or_else(|_| value.to_string_lossy().into_owned()),
119+
Some(ConstantData::Str { value }) => {
120+
value.as_str().map_or_else(
121+
|_| value.to_string_lossy().into_owned(),
122+
|s| s.to_owned(),
123+
)
124+
}
123125
Some(other) => panic!(
124126
"Unsupported annotation const for '{:?}' at idx {}: {:?}",
125127
param_name, val_idx, other

crates/sre_engine/src/string.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -339,29 +339,20 @@ const fn is_py_ascii_whitespace(b: u8) -> bool {
339339

340340
#[inline]
341341
pub(crate) fn is_word(ch: u32) -> bool {
342-
ch == '_' as u32
343-
|| u8::try_from(ch)
344-
.map(|x| x.is_ascii_alphanumeric())
345-
.unwrap_or(false)
342+
ch == '_' as u32 || u8::try_from(ch).is_ok_and(|x| x.is_ascii_alphanumeric())
346343
}
347344
#[inline]
348345
pub(crate) fn is_space(ch: u32) -> bool {
349-
u8::try_from(ch)
350-
.map(is_py_ascii_whitespace)
351-
.unwrap_or(false)
346+
u8::try_from(ch).is_ok_and(is_py_ascii_whitespace)
352347
}
353348
#[inline]
354349
pub(crate) fn is_digit(ch: u32) -> bool {
355-
u8::try_from(ch)
356-
.map(|x| x.is_ascii_digit())
357-
.unwrap_or(false)
350+
u8::try_from(ch).is_ok_and(|x| x.is_ascii_digit())
358351
}
359352
#[inline]
360353
pub(crate) fn is_loc_alnum(ch: u32) -> bool {
361354
// FIXME: Ignore the locales
362-
u8::try_from(ch)
363-
.map(|x| x.is_ascii_alphanumeric())
364-
.unwrap_or(false)
355+
u8::try_from(ch).is_ok_and(|x| x.is_ascii_alphanumeric())
365356
}
366357
#[inline]
367358
pub(crate) fn is_loc_word(ch: u32) -> bool {
@@ -374,9 +365,7 @@ pub(crate) const fn is_linebreak(ch: u32) -> bool {
374365
#[inline]
375366
#[must_use]
376367
pub fn lower_ascii(ch: u32) -> u32 {
377-
u8::try_from(ch)
378-
.map(|x| x.to_ascii_lowercase() as u32)
379-
.unwrap_or(ch)
368+
u8::try_from(ch).map_or(ch, |x| x.to_ascii_lowercase() as u32)
380369
}
381370
#[inline]
382371
pub(crate) fn lower_locate(ch: u32) -> u32 {
@@ -386,16 +375,12 @@ pub(crate) fn lower_locate(ch: u32) -> u32 {
386375
#[inline]
387376
pub(crate) fn upper_locate(ch: u32) -> u32 {
388377
// FIXME: Ignore the locales
389-
u8::try_from(ch)
390-
.map(|x| x.to_ascii_uppercase() as u32)
391-
.unwrap_or(ch)
378+
u8::try_from(ch).map_or(ch, |x| x.to_ascii_uppercase() as u32)
392379
}
393380
#[inline]
394381
pub(crate) fn is_uni_digit(ch: u32) -> bool {
395382
// TODO: check with cpython
396-
char::try_from(ch)
397-
.map(|x| x.is_ascii_digit())
398-
.unwrap_or(false)
383+
char::try_from(ch).is_ok_and(|x| x.is_ascii_digit())
399384
}
400385
#[inline]
401386
pub(crate) fn is_uni_space(ch: u32) -> bool {
@@ -444,13 +429,11 @@ pub(crate) const fn is_uni_linebreak(ch: u32) -> bool {
444429
#[inline]
445430
pub(crate) fn is_uni_alnum(ch: u32) -> bool {
446431
// TODO: check with cpython
447-
char::try_from(ch)
448-
.map(|c| {
449-
GeneralCategoryGroup::Letter
450-
.union(GeneralCategoryGroup::Number)
451-
.contains(GeneralCategory::for_char(c))
452-
})
453-
.unwrap_or(false)
432+
char::try_from(ch).is_ok_and(|c| {
433+
GeneralCategoryGroup::Letter
434+
.union(GeneralCategoryGroup::Number)
435+
.contains(GeneralCategory::for_char(c))
436+
})
454437
}
455438
#[inline]
456439
pub(crate) fn is_uni_word(ch: u32) -> bool {
@@ -460,15 +443,11 @@ pub(crate) fn is_uni_word(ch: u32) -> bool {
460443
#[must_use]
461444
pub fn lower_unicode(ch: u32) -> u32 {
462445
// TODO: check with cpython
463-
char::try_from(ch)
464-
.map(|x| x.to_lowercase().next().unwrap() as u32)
465-
.unwrap_or(ch)
446+
char::try_from(ch).map_or(ch, |x| x.to_lowercase().next().unwrap() as u32)
466447
}
467448
#[inline]
468449
#[must_use]
469450
pub fn upper_unicode(ch: u32) -> u32 {
470451
// TODO: check with cpython
471-
char::try_from(ch)
472-
.map(|x| x.to_uppercase().next().unwrap() as u32)
473-
.unwrap_or(ch)
452+
char::try_from(ch).map_or(ch, |x| x.to_uppercase().next().unwrap() as u32)
474453
}

crates/stdlib/src/_asyncio.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,7 @@ pub(crate) mod _asyncio {
900900
{
901901
let s = state
902902
.str(vm)
903-
.map(|s| s.as_wtf8().to_lowercase())
904-
.unwrap_or_else(|_| Wtf8Buf::from("unknown"));
903+
.map_or_else(|_| Wtf8Buf::from("unknown"), |s| s.as_wtf8().to_lowercase());
905904
return Ok(s);
906905
}
907906
Ok(Wtf8Buf::from("state=unknown"))

crates/stdlib/src/_opcode.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,22 @@ mod _opcode {
3434

3535
#[pyfunction]
3636
fn stack_effect(args: StackEffectArgs, vm: &VirtualMachine) -> PyResult<i32> {
37-
let oparg = args
38-
.oparg
39-
.map(|v| {
40-
if !v.fast_isinstance(vm.ctx.types.int_type) {
41-
return Err(vm.new_type_error(format!(
37+
let oparg = args.oparg.map_or(Ok(0), |v| {
38+
if !v.fast_isinstance(vm.ctx.types.int_type) {
39+
return Err(vm.new_type_error(format!(
40+
"'{}' object cannot be interpreted as an integer",
41+
v.class().name()
42+
)));
43+
}
44+
v.downcast_ref::<PyInt>()
45+
.ok_or_else(|| {
46+
vm.new_type_error(format!(
4247
"'{}' object cannot be interpreted as an integer",
4348
v.class().name()
44-
)));
45-
}
46-
v.downcast_ref::<PyInt>()
47-
.ok_or_else(|| {
48-
vm.new_type_error(format!(
49-
"'{}' object cannot be interpreted as an integer",
50-
v.class().name()
51-
))
52-
})?
53-
.try_to_primitive::<u32>(vm)
54-
})
55-
.unwrap_or(Ok(0))?;
49+
))
50+
})?
51+
.try_to_primitive::<u32>(vm)
52+
})?;
5653

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

10097
#[pyfunction]
10198
fn has_arg(opcode: i32) -> bool {
102-
try_from_i32(opcode).map(|op| op.has_arg()).unwrap_or(false)
99+
try_from_i32(opcode).is_ok_and(|op| op.has_arg())
103100
}
104101

105102
#[pyfunction]
106103
fn has_const(opcode: i32) -> bool {
107-
try_from_i32(opcode)
108-
.map(|op| op.has_const())
109-
.unwrap_or(false)
104+
try_from_i32(opcode).is_ok_and(|op| op.has_const())
110105
}
111106

112107
#[pyfunction]
113108
fn has_name(opcode: i32) -> bool {
114-
try_from_i32(opcode)
115-
.map(|op| op.has_name())
116-
.unwrap_or(false)
109+
try_from_i32(opcode).is_ok_and(|op| op.has_name())
117110
}
118111

119112
#[pyfunction]
120113
fn has_jump(opcode: i32) -> bool {
121-
try_from_i32(opcode)
122-
.map(|op| op.has_jump())
123-
.unwrap_or(false)
114+
try_from_i32(opcode).is_ok_and(|op| op.has_jump())
124115
}
125116

126117
#[pyfunction]
127118
fn has_free(opcode: i32) -> bool {
128-
try_from_i32(opcode)
129-
.map(|op| op.has_free())
130-
.unwrap_or(false)
119+
try_from_i32(opcode).is_ok_and(|op| op.has_free())
131120
}
132121

133122
#[pyfunction]
134123
fn has_local(opcode: i32) -> bool {
135-
try_from_i32(opcode)
136-
.map(|op| op.has_local())
137-
.unwrap_or(false)
124+
try_from_i32(opcode).is_ok_and(|op| op.has_local())
138125
}
139126

140127
#[pyfunction]
141128
fn has_exc(opcode: i32) -> bool {
142-
try_from_i32(opcode)
143-
.map(|op| op.is_block_push())
144-
.unwrap_or(false)
129+
try_from_i32(opcode).is_ok_and(|op| op.is_block_push())
145130
}
146131

147132
#[pyfunction]

0 commit comments

Comments
 (0)