about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-02-15 16:47:06 +0000
committerbors <bors@rust-lang.org>2020-02-15 16:47:06 +0000
commit578960d61dbc01ff2db05ea818e41414a7acc12e (patch)
treeaa511ae13c66ffaeae95bb0a8e4d1bfa33e32990
parent779b6aeaa62d7d832bb791c7d92ed11d43f4873a (diff)
parent787398aa53ef633d67433e6e89c14590b54fa4af (diff)
downloadrust-578960d61dbc01ff2db05ea818e41414a7acc12e.tar.gz
rust-578960d61dbc01ff2db05ea818e41414a7acc12e.zip
Auto merge of #5177 - matthewjasper:own-infer, r=flip1995
Avoid using regions from `TypeckTables`

These regions will all be `ReErased` soon. (rust-lang/rust#69189)

changelog: none
-rw-r--r--clippy_lints/src/methods/mod.rs39
-rw-r--r--clippy_lints/src/needless_pass_by_value.rs4
2 files changed, 39 insertions, 4 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 716cd74ab92..d8494865477 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -1721,14 +1721,49 @@ fn lint_expect_fun_call(
         if match_type(cx, arg_ty, &paths::STRING) {
             return false;
         }
-        if let ty::Ref(ty::ReStatic, ty, ..) = arg_ty.kind {
-            if ty.kind == ty::Str {
+        if let ty::Ref(_, ty, ..) = arg_ty.kind {
+            if ty.kind == ty::Str && can_be_static_str(cx, arg) {
                 return false;
             }
         };
         true
     }
 
+    // Check if an expression could have type `&'static str`, knowing that it
+    // has type `&str` for some lifetime.
+    fn can_be_static_str(cx: &LateContext<'_, '_>, arg: &hir::Expr<'_>) -> bool {
+        match arg.kind {
+            hir::ExprKind::Lit(_) => true,
+            hir::ExprKind::Call(fun, _) => {
+                if let hir::ExprKind::Path(ref p) = fun.kind {
+                    match cx.tables.qpath_res(p, fun.hir_id) {
+                        hir::def::Res::Def(hir::def::DefKind::Fn, def_id)
+                        | hir::def::Res::Def(hir::def::DefKind::Method, def_id) => matches!(
+                            cx.tcx.fn_sig(def_id).output().skip_binder().kind,
+                            ty::Ref(ty::ReStatic, ..)
+                        ),
+                        _ => false,
+                    }
+                } else {
+                    false
+                }
+            },
+            hir::ExprKind::MethodCall(..) => cx.tables.type_dependent_def_id(arg.hir_id).map_or(false, |method_id| {
+                matches!(
+                    cx.tcx.fn_sig(method_id).output().skip_binder().kind,
+                    ty::Ref(ty::ReStatic, ..)
+                )
+            }),
+            hir::ExprKind::Path(ref p) => match cx.tables.qpath_res(p, arg.hir_id) {
+                hir::def::Res::Def(hir::def::DefKind::Const, _) | hir::def::Res::Def(hir::def::DefKind::Static, _) => {
+                    true
+                },
+                _ => false,
+            },
+            _ => false,
+        }
+    }
+
     fn generate_format_arg_snippet(
         cx: &LateContext<'_, '_>,
         a: &hir::Expr<'_>,
diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs
index 0e844b92071..7bf7f21dd54 100644
--- a/clippy_lints/src/needless_pass_by_value.rs
+++ b/clippy_lints/src/needless_pass_by_value.rs
@@ -7,7 +7,7 @@ use if_chain::if_chain;
 use matches::matches;
 use rustc::traits;
 use rustc::traits::misc::can_type_implement_copy;
-use rustc::ty::{self, RegionKind, TypeFoldable};
+use rustc::ty::{self, TypeFoldable};
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_errors::{Applicability, DiagnosticBuilder};
 use rustc_hir::intravisit::FnKind;
@@ -171,7 +171,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
                 (
                     preds.iter().any(|t| t.def_id() == borrow_trait),
                     !preds.is_empty() && {
-                        let ty_empty_region = cx.tcx.mk_imm_ref(&RegionKind::ReEmpty(ty::UniverseIndex::ROOT), ty);
+                        let ty_empty_region = cx.tcx.mk_imm_ref(cx.tcx.lifetimes.re_root_empty, ty);
                         preds.iter().all(|t| {
                             let ty_params = &t
                                 .skip_binder()