about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxFrednet <xFrednet@gmail.com>2021-02-20 00:35:28 +0100
committerxFrednet <xFrednet@gmail.com>2021-05-05 18:35:33 +0200
commitee130d066d362118cd0bf756de42ee08a2bfc301 (patch)
tree3aafbcbc041a03099d0a2fc22812bae2f31950bf
parent8dca1b8f618fdec3f1b354d9bafa01b32ecc8ab6 (diff)
downloadrust-ee130d066d362118cd0bf756de42ee08a2bfc301.tar.gz
rust-ee130d066d362118cd0bf756de42ee08a2bfc301.zip
Metadata collection: Tracking Applicability mut borrows
-rw-r--r--clippy_lints/src/utils/internal_lints/metadata_collector.rs57
1 files changed, 48 insertions, 9 deletions
diff --git a/clippy_lints/src/utils/internal_lints/metadata_collector.rs b/clippy_lints/src/utils/internal_lints/metadata_collector.rs
index e22aa285f17..c8637531d94 100644
--- a/clippy_lints/src/utils/internal_lints/metadata_collector.rs
+++ b/clippy_lints/src/utils/internal_lints/metadata_collector.rs
@@ -38,7 +38,7 @@ use std::path::Path;
 use crate::utils::internal_lints::is_lint_ref_type;
 use crate::utils::{
     get_enclosing_body, get_parent_expr_for_hir, last_path_segment, match_function_call, match_qpath, match_type,
-    path_to_local_id, paths, span_lint, walk_ptrs_ty_depth,
+    path_to_local_id, paths, span_lint, walk_ptrs_ty_depth, get_parent_expr
 };
 
 /// This is the output file of the lint collector.
@@ -478,7 +478,7 @@ impl<'a, 'hir> ValueTracker<'a, 'hir> {
                     self.value_mutations.push(ApplicabilityModifier::Producer(path));
                 } else {
                     let msg = format!(
-                        "Unsupported Call expression at: {}",
+                        "Unsupported assign Call expression at: {}",
                         SerializableSpan::from_span(self.cx, func_expr.span)
                     );
                     self.value_mutations.push(ApplicabilityModifier::Unknown(msg));
@@ -486,7 +486,7 @@ impl<'a, 'hir> ValueTracker<'a, 'hir> {
             },
             hir::ExprKind::MethodCall(..) => {
                 let msg = format!(
-                    "Unsupported MethodCall expression at: {}",
+                    "Unsupported assign MethodCall expression at: {}",
                     SerializableSpan::from_span(self.cx, expr.span)
                 );
                 self.value_mutations.push(ApplicabilityModifier::Unknown(msg));
@@ -518,13 +518,56 @@ impl<'a, 'hir> ValueTracker<'a, 'hir> {
             // hir::ExprKind::Index(expr, expr) => not supported
             _ => {
                 let msg = format!(
-                    "Unexpected expression at: {}",
+                    "Unexpected assign expression at: {}",
                     SerializableSpan::from_span(self.cx, expr.span)
                 );
                 self.value_mutations.push(ApplicabilityModifier::Unknown(msg));
             },
         }
     }
+
+    fn process_borrow_expr(&mut self, access_hir_id: hir::HirId) {
+        let borrower: &rustc_hir::Expr<'_>;
+        if let Some(addr_of_expr) = get_parent_expr_for_hir(self.cx, access_hir_id) {
+            if let Some(borrower_expr) = get_parent_expr(self.cx, addr_of_expr) {
+                borrower = borrower_expr
+            } else {
+                return;
+            }
+        } else {
+            return;
+        }
+
+        match &borrower.kind {
+            hir::ExprKind::Call(func_expr, ..) => {
+                // We only deal with resolved paths as this is the usual case. Other expression kinds like closures
+                // etc. are hard to track but might be a worthy improvement in the future
+                if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = func_expr.kind {
+                    self.value_mutations.push(ApplicabilityModifier::Modifier(path));
+                } else {
+                    let msg = format!(
+                        "Unsupported borrow in Call at: {}",
+                        SerializableSpan::from_span(self.cx, func_expr.span)
+                    );
+                    self.value_mutations.push(ApplicabilityModifier::Unknown(msg));
+                }
+            },
+            hir::ExprKind::MethodCall(..) => {
+                let msg = format!(
+                    "Unsupported borrow in MethodCall at: {}",
+                    SerializableSpan::from_span(self.cx, borrower.span)
+                );
+                self.value_mutations.push(ApplicabilityModifier::Unknown(msg));
+            },
+            _ => {
+                let msg = format!(
+                    "Unexpected borrow at: {}",
+                    SerializableSpan::from_span(self.cx, borrower.span)
+                );
+                self.value_mutations.push(ApplicabilityModifier::Unknown(msg));
+            },
+        }
+    }
 }
 
 impl<'a, 'hir> Delegate<'hir> for ValueTracker<'a, 'hir> {
@@ -541,11 +584,7 @@ impl<'a, 'hir> Delegate<'hir> for ValueTracker<'a, 'hir> {
     fn borrow(&mut self, _place_with_id: &PlaceWithHirId<'hir>, expr_id: hir::HirId, bk: BorrowKind) {
         if self.is_value_expr(expr_id) {
             if let BorrowKind::MutBorrow = bk {
-                // TODO xFrednet 2021-02-17: Save the function
-                if let Some(hir::Node::Expr(expr)) = self.cx.tcx.hir().find(expr_id) {
-                    let span = SerializableSpan::from_span(self.cx, expr.span);
-                    log_to_file(&format!("- &mut     {}\n", span));
-                }
+                self.process_borrow_expr(expr_id);
             }
         }
     }