]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/utils/mod.rs
Account for trait alias when looking for defid
[rust.git] / clippy_lints / src / utils / mod.rs
index 020068d4633a3878f3ea1c766a64d30e69b5d1c8..408deb1b402d2be483da4c6622a8407e8ea1da8d 100644 (file)
@@ -92,11 +92,6 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool {
     }
 }
 
-/// Returns `true` if this `span` was expanded by any macro or desugaring
-pub fn in_macro_or_desugar(span: Span) -> bool {
-    span.from_expansion()
-}
-
 /// Returns `true` if this `span` was expanded by any macro.
 pub fn in_macro(span: Span) -> bool {
     if span.from_expansion() {
@@ -266,6 +261,7 @@ pub fn path_to_res(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<(def::Res)
 }
 
 /// Convenience function to get the `DefId` of a trait by path.
+/// It could be a trait or trait alias.
 pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId> {
     let res = match path_to_res(cx, path) {
         Some(res) => res,
@@ -273,7 +269,8 @@ pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId
     };
 
     match res {
-        def::Res::Def(DefKind::Trait, trait_id) => Some(trait_id),
+        Res::Def(DefKind::Trait, trait_id) | Res::Def(DefKind::TraitAlias, trait_id) => Some(trait_id),
+        Res::Err => unreachable!("this trait resolution is impossible: {:?}", &path),
         _ => None,
     }
 }
@@ -349,7 +346,7 @@ pub fn method_calls(expr: &Expr, max_depth: usize) -> (Vec<Symbol>, Vec<&[Expr]>
     let mut current = expr;
     for _ in 0..max_depth {
         if let ExprKind::MethodCall(path, _, args) = &current.node {
-            if args.iter().any(|e| in_macro_or_desugar(e.span)) {
+            if args.iter().any(|e| e.span.from_expansion()) {
                 break;
             }
             method_names.push(path.ident.name);
@@ -376,7 +373,7 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
         // method chains are stored last -> first
         if let ExprKind::MethodCall(ref path, _, ref args) = current.node {
             if path.ident.name.as_str() == *method_name {
-                if args.iter().any(|e| in_macro_or_desugar(e.span)) {
+                if args.iter().any(|e| e.span.from_expansion()) {
                     return None;
                 }
                 matched.push(&**args); // build up `matched` backwards
@@ -471,7 +468,7 @@ pub fn snippet_with_applicability<'a, T: LintContext>(
     default: &'a str,
     applicability: &mut Applicability,
 ) -> Cow<'a, str> {
-    if *applicability != Applicability::Unspecified && in_macro_or_desugar(span) {
+    if *applicability != Applicability::Unspecified && span.from_expansion() {
         *applicability = Applicability::MaybeIncorrect;
     }
     snippet_opt(cx, span).map_or_else(
@@ -536,7 +533,7 @@ pub fn last_line_of_span<T: LintContext>(cx: &T, span: Span) -> Span {
 pub fn expr_block<'a, T: LintContext>(cx: &T, expr: &Expr, option: Option<String>, default: &'a str) -> Cow<'a, str> {
     let code = snippet_block(cx, expr.span, default);
     let string = option.unwrap_or_default();
-    if in_macro_or_desugar(expr.span) {
+    if expr.span.from_expansion() {
         Cow::Owned(format!("{{ {} }}", snippet_with_macro_callsite(cx, expr.span, default)))
     } else if let ExprKind::Block(_, _) = expr.node {
         Cow::Owned(format!("{}{}", code, string))