Skip to content

Commit 475f554

Browse files
committed
lint: fix clippy::map_unwrap_or
1 parent 5e37b43 commit 475f554

File tree

27 files changed

+114
-157
lines changed

27 files changed

+114
-157
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ inline_always = "allow" # TODO: benchmark inlines
2121
missing_panics_doc = "allow" # TODO
2222
similar_names = "allow" # 26
2323
float_cmp = "allow" # 41
24-
map_unwrap_or = "allow" # 56
2524
return_self_not_must_use = "allow" # 63
2625
needless_pass_by_value = "allow" # 70
2726
redundant_closure_for_method_calls = "allow" # 76

pad/editor/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl State {
126126
return;
127127
};
128128
let code = get_code(&self.code_id);
129-
let before = self.past.last().map(|r| r.after).unwrap_or(cursor);
129+
let before = self.past.last().map_or(cursor, |r| r.after);
130130
let new_curr = Record {
131131
code: code.clone(),
132132
before,

parser/src/ast.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,8 @@ pub struct ImportLine {
165165
impl Import {
166166
/// The full span of the import
167167
pub fn span(&self) -> CodeSpan {
168-
let first = (self.name.as_ref())
169-
.map(|n| n.span.clone())
170-
.unwrap_or_else(|| self.path.span.clone());
171-
let last = (self.items().last())
172-
.map(|i| i.span.clone())
173-
.unwrap_or_else(|| self.path.span.clone());
168+
let first = (self.name.as_ref()).map_or_else(|| self.path.span.clone(), |n| n.span.clone());
169+
let last = (self.items().last()).map_or_else(|| self.path.span.clone(), |i| i.span.clone());
174170
first.merge(last)
175171
}
176172
/// The imported items
@@ -249,16 +245,14 @@ pub struct FieldInit {
249245
impl DataDef {
250246
/// Get the span of this data definition
251247
pub fn span(&self) -> CodeSpan {
252-
let end = self
253-
.fields
254-
.as_ref()
255-
.map(|fields| fields.span())
256-
.unwrap_or_else(|| {
248+
let end = self.fields.as_ref().map_or_else(
249+
|| {
257250
self.name
258251
.as_ref()
259-
.map(|name| name.span.clone())
260-
.unwrap_or_else(|| self.init_span.clone())
261-
});
252+
.map_or_else(|| self.init_span.clone(), |name| name.span.clone())
253+
},
254+
|fields| fields.span(),
255+
);
262256
let mut span = (self.init_span.clone()).merge(end);
263257
if let Some(words) = &self.func {
264258
if let Some(word) = words.last() {
@@ -295,8 +289,7 @@ impl DataField {
295289
self.init.as_ref().map(|d| {
296290
d.words
297291
.last()
298-
.map(|w| w.span.clone())
299-
.unwrap_or_else(|| d.arrow_span.clone())
292+
.map_or_else(|| d.arrow_span.clone(), |w| w.span.clone())
300293
})
301294
}) else {
302295
return self.name.span.clone();

site/src/blog.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ struct BlogParam(String);
1717
impl IntoParam for BlogParam {
1818
fn into_param(value: Option<&str>, _: &str) -> Result<Self, ParamsError> {
1919
let s = value.unwrap_or_default();
20-
let name = urlencoding::decode(s)
21-
.map(Into::into)
22-
.unwrap_or_else(|_| s.into());
20+
let name = urlencoding::decode(s).map_or_else(|_| s.into(), Into::into);
2321
Ok(BlogParam(name))
2422
}
2523
}

site/src/docs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ fn DocsHome(#[prop(optional)] search: String) -> impl IntoView {
198198
}
199199
};
200200
let update_title = move || {
201-
current_prim
202-
.get()
203-
.map(|p| format!("{} - {} Docs", lang(), p.name()))
204-
.unwrap_or_else(|| format!("{} Docs", lang()))
201+
current_prim.get().map_or_else(
202+
|| format!("{} Docs", lang()),
203+
|p| format!("{} - {} Docs", lang(), p.name()),
204+
)
205205
};
206206

207207
set_timeout(

site/src/markdown.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,13 @@ fn node_view<'a>(node: &'a AstNode<'a>) -> View {
153153
}
154154
NodeValue::Link(link) => {
155155
let text = leaf_text(node).unwrap_or_default();
156-
let name = text
157-
.rsplit_once(' ')
158-
.map(|(a, b)| {
159-
if a.chars().count() > b.chars().count() {
160-
a
161-
} else {
162-
b
163-
}
164-
})
165-
.unwrap_or(&text);
156+
let name = text.rsplit_once(' ').map_or(text.as_str(), |(a, b)| {
157+
if a.chars().count() > b.chars().count() {
158+
a
159+
} else {
160+
b
161+
}
162+
});
166163
if let Some(prim) = Primitive::from_name(name).or_else(|| Primitive::from_name(&text)) {
167164
view!(<Prim prim=prim/>).into_view()
168165
} else {
@@ -278,16 +275,13 @@ fn node_html<'a>(node: &'a AstNode<'a>) -> String {
278275
}
279276
NodeValue::Link(link) => {
280277
let text = leaf_text(node).unwrap_or_default();
281-
let name = text
282-
.rsplit_once(' ')
283-
.map(|(a, b)| {
284-
if a.chars().count() > b.chars().count() {
285-
a
286-
} else {
287-
b
288-
}
289-
})
290-
.unwrap_or(&text);
278+
let name = text.rsplit_once(' ').map_or(text.as_str(), |(a, b)| {
279+
if a.chars().count() > b.chars().count() {
280+
a
281+
} else {
282+
b
283+
}
284+
});
291285
if let Some(prim) = Primitive::from_name(name).or_else(|| Primitive::from_name(&text)) {
292286
let symbol_class = format!("prim-glyph {}", prim_class(prim));
293287
let symbol = prim.to_string();
@@ -332,8 +326,7 @@ fn node_html<'a>(node: &'a AstNode<'a>) -> String {
332326
.map(|s| {
333327
s.chars()
334328
.position(|c| c == '#')
335-
.map(|i| i + 1)
336-
.unwrap_or_else(|| s.chars().count() + 2)
329+
.map_or_else(|| s.chars().count() + 2, |i| i + 1)
337330
})
338331
.max()
339332
.unwrap_or(0);

site/src/tutorial.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ impl IntoParam for TutorialPage {
153153
fn TutorialNav(page: TutorialPage) -> impl IntoView {
154154
let next = move || {
155155
page.next()
156-
.map(|p| {
157-
view!( <div>"Next: "<A href=format!("/tutorial/{}", p.path())>{p.title()}</A>" 〉"</div>)
158-
.into_view()
156+
.map_or_else(
157+
|| view!( <div/>).into_view(),
158+
|p| {
159+
view!( <div>"Next: "<A href=format!("/tutorial/{}", p.path())>{p.title()}</A>" 〉"</div>).into_view()
159160
})
160-
.unwrap_or_else(|| view!( <div/>).into_view())
161161
};
162162
let previous = move || {
163163
page.previous()
164-
.map(|p| {
165-
view!( <div>"〈 Previous: "<A href=format!("/tutorial/{}", p.path())>{p.title()}</A></div>)
166-
.into_view()
164+
.map_or_else(
165+
|| view!( <div/>).into_view(),
166+
|p| {
167+
view!( <div>"〈 Previous: "<A href=format!("/tutorial/{}", p.path())>{p.title()}</A></div>).into_view()
167168
})
168-
.unwrap_or_else(|| view!( <div/>).into_view())
169169
};
170170

171171
view! {

src/algorithm/dyadic/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,10 +1923,7 @@ impl<T: RealArrayValue> Array<T> {
19231923
let slice = data.make_mut();
19241924
let mut bases = bases.to_vec();
19251925
let count = row_len.saturating_sub(bases.len());
1926-
bases.extend(repeat_n(
1927-
fill.as_ref().map(|fv| fv.value).unwrap_or(1.0),
1928-
count,
1929-
));
1926+
bases.extend(repeat_n(fill.as_ref().map_or(1.0, |fv| fv.value), count));
19301927
if fill.is_some_and(|fv| fv.is_left()) {
19311928
bases.rotate_right(count);
19321929
}

src/algorithm/dyadic/search.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ impl<T: ArrayValue> Array<T> {
182182
for elem in needle.row_slices() {
183183
let index = (haystack.row_slices())
184184
.position(|row| ArrayCmpSlice(row) == ArrayCmpSlice(elem))
185-
.map(|i| i as f64)
186-
.unwrap_or(default);
185+
.map_or(default, |i| i as f64);
187186
result_data.push(index);
188187
}
189188
} else {
@@ -254,8 +253,7 @@ impl<T: ArrayValue> Array<T> {
254253
r.len() == needle.data.len()
255254
&& r.iter().zip(&needle.data).all(|(a, b)| a.array_eq(b))
256255
})
257-
.map(|i| i as f64)
258-
.unwrap_or(default))
256+
.map_or(default, |i| i as f64))
259257
.into()
260258
}
261259
} else {
@@ -290,7 +288,7 @@ impl<T: ArrayValue> Array<T> {
290288
let mut cache = HashMap::with_capacity(needle.row_count());
291289
'needle: for elem in needle.row_slices() {
292290
let elem_key = ArrayCmpSlice(elem);
293-
let start_index = cache.get(&elem_key).map(|&i| i + 1).unwrap_or(0);
291+
let start_index = cache.get(&elem_key).map_or(0, |&i| i + 1);
294292
for i in start_index..haystack.row_count() {
295293
let of_key = ArrayCmpSlice(haystack.row_slice(i));
296294
if of_key == elem_key {
@@ -365,8 +363,7 @@ impl<T: ArrayValue> Array<T> {
365363
r.len() == needle.data.len()
366364
&& r.iter().zip(&needle.data).all(|(a, b)| a.array_eq(b))
367365
})
368-
.map(|i| i as f64)
369-
.unwrap_or(default))
366+
.map_or(default, |i| i as f64))
370367
.into()
371368
} else {
372369
let mut rows = Vec::with_capacity(haystack.row_count());

src/algorithm/dyadic/structure.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,7 @@ impl<T: ArrayValue> Array<T> {
894894
fn anti_drop(mut self, mut index: &[isize], env: &Uiua) -> UiuaResult<Self> {
895895
let fill = env
896896
.array_fill::<T>()
897-
.map(|fv| fv.value)
898-
.unwrap_or_else(|_| T::proxy().into());
897+
.map_or_else(|_| T::proxy().into(), |fv| fv.value);
899898
if self.shape.len() < index.len() {
900899
return Err(env.error(format!(
901900
"Index array specifies {} axes, \
@@ -1526,11 +1525,7 @@ impl<T: ArrayValue> Array<T> {
15261525
.iter()
15271526
.map(|&i| normalize_index(i, indices.len()))
15281527
.collect();
1529-
let row_count = normalized_indices
1530-
.iter()
1531-
.max()
1532-
.map(|&max| max + 1)
1533-
.unwrap_or(0);
1528+
let row_count = normalized_indices.iter().max().map_or(0, |&max| max + 1);
15341529
// Check indices totality
15351530
let indices_are_total = indices_are_total(indices, row_count);
15361531
// Get fill if not total

0 commit comments

Comments
 (0)