Skip to content

Commit f09650b

Browse files
committed
Use symbols when raising range expressions.
1 parent 9f5d60f commit f09650b

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

clippy_utils/src/higher.rs

+34-29
Original file line numberDiff line numberDiff line change
@@ -218,51 +218,56 @@ pub struct Range<'a> {
218218

219219
impl<'a> Range<'a> {
220220
/// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
221+
#[allow(clippy::similar_names)]
221222
pub fn hir(expr: &'a Expr<'_>) -> Option<Range<'a>> {
222-
/// Finds the field named `name` in the field. Always return `Some` for
223-
/// convenience.
224-
fn get_field<'c>(name: &str, fields: &'c [hir::ExprField<'_>]) -> Option<&'c Expr<'c>> {
225-
let expr = &fields.iter().find(|field| field.ident.name.as_str() == name)?.expr;
226-
Some(expr)
227-
}
228-
229223
match expr.kind {
230-
ExprKind::Call(path, args)
224+
ExprKind::Call(path, [arg1, arg2])
231225
if matches!(
232226
path.kind,
233227
ExprKind::Path(QPath::LangItem(hir::LangItem::RangeInclusiveNew, ..))
234228
) =>
235229
{
236230
Some(Range {
237-
start: Some(&args[0]),
238-
end: Some(&args[1]),
231+
start: Some(arg1),
232+
end: Some(arg2),
239233
limits: ast::RangeLimits::Closed,
240234
})
241235
},
242-
ExprKind::Struct(path, fields, None) => match &path {
243-
QPath::LangItem(hir::LangItem::RangeFull, ..) => Some(Range {
236+
ExprKind::Struct(path, fields, None) => match (path, fields) {
237+
(QPath::LangItem(hir::LangItem::RangeFull, ..), []) => Some(Range {
244238
start: None,
245239
end: None,
246240
limits: ast::RangeLimits::HalfOpen,
247241
}),
248-
QPath::LangItem(hir::LangItem::RangeFrom, ..) => Some(Range {
249-
start: Some(get_field("start", fields)?),
250-
end: None,
251-
limits: ast::RangeLimits::HalfOpen,
252-
}),
253-
QPath::LangItem(hir::LangItem::Range, ..) => Some(Range {
254-
start: Some(get_field("start", fields)?),
255-
end: Some(get_field("end", fields)?),
256-
limits: ast::RangeLimits::HalfOpen,
257-
}),
258-
QPath::LangItem(hir::LangItem::RangeToInclusive, ..) => Some(Range {
259-
start: None,
260-
end: Some(get_field("end", fields)?),
261-
limits: ast::RangeLimits::Closed,
262-
}),
263-
QPath::LangItem(hir::LangItem::RangeTo, ..) => Some(Range {
242+
(QPath::LangItem(hir::LangItem::RangeFrom, ..), [field]) if field.ident.name == sym::start => {
243+
Some(Range {
244+
start: Some(field.expr),
245+
end: None,
246+
limits: ast::RangeLimits::HalfOpen,
247+
})
248+
},
249+
(QPath::LangItem(hir::LangItem::Range, ..), [field1, field2]) => {
250+
let (start, end) = match (field1.ident.name, field2.ident.name) {
251+
(sym::start, sym::end) => (field1.expr, field2.expr),
252+
(sym::end, sym::start) => (field2.expr, field1.expr),
253+
_ => return None,
254+
};
255+
Some(Range {
256+
start: Some(start),
257+
end: Some(end),
258+
limits: ast::RangeLimits::HalfOpen,
259+
})
260+
},
261+
(QPath::LangItem(hir::LangItem::RangeToInclusive, ..), [field]) if field.ident.name == sym::end => {
262+
Some(Range {
263+
start: None,
264+
end: Some(field.expr),
265+
limits: ast::RangeLimits::Closed,
266+
})
267+
},
268+
(QPath::LangItem(hir::LangItem::RangeTo, ..), [field]) if field.ident.name == sym::end => Some(Range {
264269
start: None,
265-
end: Some(get_field("end", fields)?),
270+
end: Some(field.expr),
266271
limits: ast::RangeLimits::HalfOpen,
267272
}),
268273
_ => None,

0 commit comments

Comments
 (0)