Make use of slice pattern

This commit is contained in:
Lzu Tao 2020-06-02 21:42:33 +07:00
parent bfafb8e0d6
commit fbf0b84b32

View file

@ -165,8 +165,8 @@ pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr<'_>, path: &[&st
/// Checks if an expression references a variable of the given name. /// Checks if an expression references a variable of the given name.
pub fn match_var(expr: &Expr<'_>, var: Name) -> bool { pub fn match_var(expr: &Expr<'_>, var: Name) -> bool {
if let ExprKind::Path(QPath::Resolved(None, ref path)) = expr.kind { if let ExprKind::Path(QPath::Resolved(None, ref path)) = expr.kind {
if path.segments.len() == 1 && path.segments[0].ident.name == var { if let [p] = path.segments {
return true; return p.ident.name == var;
} }
} }
false false
@ -181,8 +181,7 @@ pub fn last_path_segment<'tcx>(path: &QPath<'tcx>) -> &'tcx PathSegment<'tcx> {
pub fn single_segment_path<'tcx>(path: &QPath<'tcx>) -> Option<&'tcx PathSegment<'tcx>> { pub fn single_segment_path<'tcx>(path: &QPath<'tcx>) -> Option<&'tcx PathSegment<'tcx>> {
match *path { match *path {
QPath::Resolved(_, ref path) if path.segments.len() == 1 => Some(&path.segments[0]), QPath::Resolved(_, ref path) => path.segments.get(0),
QPath::Resolved(..) => None,
QPath::TypeRelative(_, ref seg) => Some(seg), QPath::TypeRelative(_, ref seg) => Some(seg),
} }
} }
@ -201,9 +200,12 @@ pub fn match_qpath(path: &QPath<'_>, segments: &[&str]) -> bool {
QPath::Resolved(_, ref path) => match_path(path, segments), QPath::Resolved(_, ref path) => match_path(path, segments),
QPath::TypeRelative(ref ty, ref segment) => match ty.kind { QPath::TypeRelative(ref ty, ref segment) => match ty.kind {
TyKind::Path(ref inner_path) => { TyKind::Path(ref inner_path) => {
!segments.is_empty() if let [prefix @ .., end] = segments {
&& match_qpath(inner_path, &segments[..(segments.len() - 1)]) if match_qpath(inner_path, prefix) {
&& segment.ident.name.as_str() == segments[segments.len() - 1] return segment.ident.name.as_str() == *end;
}
}
false
}, },
_ => false, _ => false,
}, },