Skip to content

Commit 726176e

Browse files
authored
Merge pull request #4003 from rust-lang/stable-backports
Backport some things to stable
2 parents 1fac380 + 5ab87dd commit 726176e

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ rustc_tools_util = { version = "0.1.1", path = "rustc_tools_util"}
4747
[dev-dependencies]
4848
clippy_dev = { version = "0.0.1", path = "clippy_dev" }
4949
cargo_metadata = "0.7.1"
50-
compiletest_rs = "0.3.18"
50+
compiletest_rs = "=0.3.18"
5151
lazy_static = "1.0"
5252
serde_derive = "1.0"
5353
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }

clippy_lints/src/eta_reduction.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,30 @@ fn get_ufcs_type_name(
129129
let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id).sty;
130130

131131
if let Some(trait_id) = cx.tcx.trait_of_item(method_def_id) {
132-
//if the method expectes &self, ufcs requires explicit borrowing so closure can't be removed
133-
return match (expected_type_of_self, actual_type_of_self) {
134-
(ty::Ref(_, _, _), ty::Ref(_, _, _)) => Some(cx.tcx.item_path_str(trait_id)),
135-
(l, r) => match (l, r) {
136-
(ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _)) => None,
137-
(_, _) => Some(cx.tcx.item_path_str(trait_id)),
138-
},
139-
};
132+
if match_borrow_depth(expected_type_of_self, actual_type_of_self) {
133+
return Some(cx.tcx.item_path_str(trait_id));
134+
}
140135
}
141136

142137
cx.tcx.impl_of_method(method_def_id).and_then(|_| {
143-
//a type may implicitly implement other types methods (e.g. Deref)
138+
//a type may implicitly implement other type's methods (e.g. Deref)
144139
if match_types(expected_type_of_self, actual_type_of_self) {
145140
return Some(get_type_name(cx, &actual_type_of_self));
146141
}
147142
None
148143
})
149144
}
150145

146+
fn match_borrow_depth(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool {
147+
match (lhs, rhs) {
148+
(ty::Ref(_, t1, _), ty::Ref(_, t2, _)) => match_borrow_depth(&t1.sty, &t2.sty),
149+
(l, r) => match (l, r) {
150+
(ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _)) => false,
151+
(_, _) => true,
152+
},
153+
}
154+
}
155+
151156
fn match_types(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool {
152157
match (lhs, rhs) {
153158
(ty::Bool, ty::Bool)

clippy_lints/src/functions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
150150

151151
let nodeid = cx.tcx.hir().hir_to_node_id(hir_id);
152152
self.check_raw_ptr(cx, unsafety, decl, body, nodeid);
153-
self.check_line_number(cx, span);
153+
self.check_line_number(cx, span, body);
154154
}
155155

156156
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
@@ -181,12 +181,12 @@ impl<'a, 'tcx> Functions {
181181
}
182182
}
183183

184-
fn check_line_number(self, cx: &LateContext<'_, '_>, span: Span) {
184+
fn check_line_number(self, cx: &LateContext<'_, '_>, span: Span, body: &'tcx hir::Body) {
185185
if in_external_macro(cx.sess(), span) {
186186
return;
187187
}
188188

189-
let code_snippet = snippet(cx, span, "..");
189+
let code_snippet = snippet(cx, body.value.span, "..");
190190
let mut line_count: u64 = 0;
191191
let mut in_comment = false;
192192
let mut code_in_line;

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::utils::{is_entrypoint_fn, span_lint};
2+
use if_chain::if_chain;
23
use rustc::hir;
34
use rustc::hir::intravisit::FnKind;
45
use rustc::hir::{Body, Constness, FnDecl, HirId};
5-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6+
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass};
67
use rustc::{declare_tool_lint, lint_array};
78
use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
89
use syntax_pos::Span;
@@ -82,7 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
8283
) {
8384
let def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
8485

85-
if is_entrypoint_fn(cx, def_id) {
86+
if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id) {
8687
return;
8788
}
8889

@@ -95,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
9596
}
9697
},
9798
FnKind::Method(_, sig, ..) => {
98-
if already_const(sig.header) {
99+
if is_trait_method(cx, hir_id) || already_const(sig.header) {
99100
return;
100101
}
101102
},
@@ -114,6 +115,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
114115
}
115116
}
116117

118+
fn is_trait_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> bool {
119+
// Get the implemented trait for the current function
120+
let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
121+
if_chain! {
122+
if parent_impl != hir::CRATE_HIR_ID;
123+
if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
124+
if let hir::ItemKind::Impl(_, _, _, _, Some(_trait_ref), _, _) = &item.node;
125+
then { return true; }
126+
}
127+
false
128+
}
129+
117130
// We don't have to lint on something that's already `const`
118131
fn already_const(header: hir::FnHeader) -> bool {
119132
header.constness == Constness::Const

tests/ui/crashes/ice-3747.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// Test for https://github.com/rust-lang/rust-clippy/issues/3747
2+
3+
macro_rules! a {
4+
( $pub:tt $($attr:tt)* ) => {
5+
$($attr)* $pub fn say_hello() {}
6+
};
7+
}
8+
9+
macro_rules! b {
10+
() => {
11+
a! { pub }
12+
};
13+
}
14+
15+
b! {}
16+
17+
fn main() {}

tests/ui/eta.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ fn test_redundant_closures_containing_method_calls() {
8888
let c = Some(TestStruct { some_ref: &i })
8989
.as_ref()
9090
.map(|c| c.to_ascii_uppercase());
91+
92+
fn test_different_borrow_levels<T>(t: &[&T])
93+
where
94+
T: TestTrait,
95+
{
96+
t.iter().filter(|x| x.trait_foo_ref());
97+
t.iter().map(|x| x.trait_foo_ref());
98+
}
9199
}
92100

93101
fn meta<F>(f: F)

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,16 @@ trait Foo {
5555
33
5656
}
5757
}
58+
59+
// Don't lint in external macros (derive)
60+
#[derive(PartialEq, Eq)]
61+
struct Point(isize, isize);
62+
63+
impl std::ops::Add for Point {
64+
type Output = Self;
65+
66+
// Don't lint in trait impls of derived methods
67+
fn add(self, other: Self) -> Self {
68+
Point(self.0 + other.0, self.1 + other.1)
69+
}
70+
}

0 commit comments

Comments
 (0)