about summary refs log tree commit diff
path: root/compiler/rustc_lint/src
diff options
context:
space:
mode:
authorMahdi Dibaiee <mdibaiee@pm.me>2022-01-11 19:59:06 +0000
committerMahdi Dibaiee <mdibaiee@pm.me>2022-01-11 19:59:06 +0000
commit959bf2bc2e79defd0fe7d3c9987a6023eb8503cd (patch)
treeb0e1b5402b5091c4c9132c8fa126302b91f67e3a /compiler/rustc_lint/src
parenta6762e962e50d5b6f864e33ebb27878e90651f22 (diff)
downloadrust-959bf2bc2e79defd0fe7d3c9987a6023eb8503cd.tar.gz
rust-959bf2bc2e79defd0fe7d3c9987a6023eb8503cd.zip
rustc_pass_by_value: handle generic and const type parameters
Diffstat (limited to 'compiler/rustc_lint/src')
-rw-r--r--compiler/rustc_lint/src/pass_by_value.rs33
1 files changed, 19 insertions, 14 deletions
diff --git a/compiler/rustc_lint/src/pass_by_value.rs b/compiler/rustc_lint/src/pass_by_value.rs
index 00b023c26f3..3435a5a6c82 100644
--- a/compiler/rustc_lint/src/pass_by_value.rs
+++ b/compiler/rustc_lint/src/pass_by_value.rs
@@ -51,15 +51,13 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
         match path.res {
             Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
                 let name = cx.tcx.item_name(def_id).to_ident_string();
-                return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
+                let path_segment = path.segments.last().unwrap();
+                return Some(format!("{}{}", name, gen_args(cx, path_segment)));
             }
             Res::SelfTy(None, Some((did, _))) => {
                 if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
                     if cx.tcx.has_attr(adt.did, sym::rustc_pass_by_value) {
-                        let name = cx.tcx.item_name(adt.did).to_ident_string();
-                        let param =
-                            substs.first().map(|s| format!("<{}>", s)).unwrap_or("".to_string());
-                        return Some(format!("{}{}", name, param));
+                        return Some(cx.tcx.def_path_str_with_substs(adt.did, substs));
                     }
                 }
             }
@@ -70,22 +68,29 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
     None
 }
 
-fn gen_args(segment: &PathSegment<'_>) -> String {
+fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
     if let Some(args) = &segment.args {
-        let lifetimes = args
+        let params = args
             .args
             .iter()
-            .filter_map(|arg| {
-                if let GenericArg::Lifetime(lt) = arg {
-                    Some(lt.name.ident().to_string())
-                } else {
-                    None
+            .filter_map(|arg| match arg {
+                GenericArg::Lifetime(lt) => Some(lt.name.ident().to_string()),
+                GenericArg::Type(ty) => {
+                    let snippet =
+                        cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default();
+                    Some(snippet)
                 }
+                GenericArg::Const(c) => {
+                    let snippet =
+                        cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default();
+                    Some(snippet)
+                }
+                _ => None,
             })
             .collect::<Vec<_>>();
 
-        if !lifetimes.is_empty() {
-            return format!("<{}>", lifetimes.join(", "));
+        if !params.is_empty() {
+            return format!("<{}>", params.join(", "));
         }
     }