Skip to content

Commit d801044

Browse files
committed
submodules: update clippy from 326b220 to 7907abe
Changes: ```` Rustup to rust-lang/rust#70634 Update clippy_lints/src/types.rs Update types.rs Update types.rs Improve docs for option_option useless Rc<Rc<T>>, Rc<Box<T>>, Rc<&T>, Box<&T> Allow let_underscore Update option_option ui test Test for ignoring let_underscore_must_use Downgrade option_option to pedantic ```` Fixes #70709
1 parent 2b3f519 commit d801044

17 files changed

+312
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,7 @@ Released 2018-09-13
14331433
[`range_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one
14341434
[`range_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_step_by_zero
14351435
[`range_zip_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_zip_with_len
1436+
[`redundant_allocation`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
14361437
[`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
14371438
[`redundant_closure`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
14381439
[`redundant_closure_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call

clippy_lints/src/escape.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use rustc_hir::intravisit;
22
use rustc_hir::{self, Body, FnDecl, HirId, HirIdSet, ItemKind, Node};
33
use rustc_infer::infer::TyCtxtInferExt;
44
use rustc_lint::{LateContext, LateLintPass};
5-
use rustc_middle::ty::layout::LayoutOf;
65
use rustc_middle::ty::{self, Ty};
76
use rustc_session::{declare_tool_lint, impl_lint_pass};
87
use rustc_span::source_map::Span;
8+
use rustc_target::abi::LayoutOf;
99
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
1010

1111
use crate::utils::span_lint;

clippy_lints/src/large_enum_variant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::utils::{snippet_opt, span_lint_and_then};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Item, ItemKind, VariantData};
66
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::ty::layout::LayoutOf;
87
use rustc_session::{declare_tool_lint, impl_lint_pass};
8+
use rustc_target::abi::LayoutOf;
99

1010
declare_clippy_lint! {
1111
/// **What it does:** Checks for large size differences between variants on

clippy_lints/src/let_underscore.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use if_chain::if_chain;
2-
use rustc_hir::{PatKind, Stmt, StmtKind};
2+
use rustc_hir::{Local, PatKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_middle::lint::in_external_macro;
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -66,13 +66,12 @@ const SYNC_GUARD_PATHS: [&[&str]; 3] = [
6666
];
6767

6868
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
69-
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &Stmt<'_>) {
70-
if in_external_macro(cx.tcx.sess, stmt.span) {
69+
fn check_local(&mut self, cx: &LateContext<'_, '_>, local: &Local<'_>) {
70+
if in_external_macro(cx.tcx.sess, local.span) {
7171
return;
7272
}
7373

7474
if_chain! {
75-
if let StmtKind::Local(ref local) = stmt.kind;
7675
if let PatKind::Wild = local.pat.kind;
7776
if let Some(ref init) = local.init;
7877
then {
@@ -81,7 +80,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
8180
span_lint_and_help(
8281
cx,
8382
LET_UNDERSCORE_LOCK,
84-
stmt.span,
83+
local.span,
8584
"non-binding let on a synchronization lock",
8685
"consider using an underscore-prefixed named \
8786
binding or dropping explicitly with `std::mem::drop`"
@@ -90,15 +89,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
9089
span_lint_and_help(
9190
cx,
9291
LET_UNDERSCORE_MUST_USE,
93-
stmt.span,
92+
local.span,
9493
"non-binding let on an expression with `#[must_use]` type",
9594
"consider explicitly using expression value"
9695
)
9796
} else if is_must_use_func_call(cx, init) {
9897
span_lint_and_help(
9998
cx,
10099
LET_UNDERSCORE_MUST_USE,
101-
stmt.span,
100+
local.span,
102101
"non-binding let on a result of a `#[must_use]` function",
103102
"consider explicitly using function result"
104103
)

clippy_lints/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
811811
&types::LET_UNIT_VALUE,
812812
&types::LINKEDLIST,
813813
&types::OPTION_OPTION,
814+
&types::REDUNDANT_ALLOCATION,
814815
&types::TYPE_COMPLEXITY,
815816
&types::UNIT_ARG,
816817
&types::UNIT_CMP,
@@ -1125,6 +1126,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11251126
LintId::of(&types::CAST_SIGN_LOSS),
11261127
LintId::of(&types::INVALID_UPCAST_COMPARISONS),
11271128
LintId::of(&types::LINKEDLIST),
1129+
LintId::of(&types::OPTION_OPTION),
11281130
LintId::of(&unicode::NON_ASCII_LITERAL),
11291131
LintId::of(&unicode::UNICODE_NOT_NFC),
11301132
LintId::of(&unused_self::UNUSED_SELF),
@@ -1375,7 +1377,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13751377
LintId::of(&types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION),
13761378
LintId::of(&types::IMPLICIT_HASHER),
13771379
LintId::of(&types::LET_UNIT_VALUE),
1378-
LintId::of(&types::OPTION_OPTION),
1380+
LintId::of(&types::REDUNDANT_ALLOCATION),
13791381
LintId::of(&types::TYPE_COMPLEXITY),
13801382
LintId::of(&types::UNIT_ARG),
13811383
LintId::of(&types::UNIT_CMP),
@@ -1565,7 +1567,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15651567
LintId::of(&transmute::TRANSMUTE_PTR_TO_REF),
15661568
LintId::of(&types::BORROWED_BOX),
15671569
LintId::of(&types::CHAR_LIT_AS_U8),
1568-
LintId::of(&types::OPTION_OPTION),
15691570
LintId::of(&types::TYPE_COMPLEXITY),
15701571
LintId::of(&types::UNIT_ARG),
15711572
LintId::of(&types::UNNECESSARY_CAST),
@@ -1661,6 +1662,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16611662
LintId::of(&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
16621663
LintId::of(&trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF),
16631664
LintId::of(&types::BOX_VEC),
1665+
LintId::of(&types::REDUNDANT_ALLOCATION),
16641666
LintId::of(&vec::USELESS_VEC),
16651667
]);
16661668

clippy_lints/src/types.rs

Lines changed: 117 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use rustc_hir::{
1717
use rustc_lint::{LateContext, LateLintPass, LintContext};
1818
use rustc_middle::hir::map::Map;
1919
use rustc_middle::lint::in_external_macro;
20-
use rustc_middle::ty::layout::LayoutOf;
2120
use rustc_middle::ty::{self, InferTy, Ty, TyCtxt, TypeckTables};
2221
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
2322
use rustc_span::hygiene::{ExpnKind, MacroKind};
2423
use rustc_span::source_map::Span;
2524
use rustc_span::symbol::{sym, Symbol};
25+
use rustc_target::abi::LayoutOf;
2626
use rustc_target::spec::abi::Abi;
2727
use rustc_typeck::hir_ty_to_ty;
2828

@@ -99,16 +99,33 @@ declare_clippy_lint! {
9999
/// represents an optional optional value which is logically the same thing as an optional
100100
/// value but has an unneeded extra level of wrapping.
101101
///
102+
/// If you have a case where `Some(Some(_))`, `Some(None)` and `None` are distinct cases,
103+
/// consider a custom `enum` instead, with clear names for each case.
104+
///
102105
/// **Known problems:** None.
103106
///
104107
/// **Example**
105108
/// ```rust
106-
/// fn x() -> Option<Option<u32>> {
109+
/// fn get_data() -> Option<Option<u32>> {
107110
/// None
108111
/// }
109112
/// ```
113+
///
114+
/// Better:
115+
///
116+
/// ```rust
117+
/// pub enum Contents {
118+
/// Data(Vec<u8>), // Was Some(Some(Vec<u8>))
119+
/// NotYetFetched, // Was Some(None)
120+
/// None, // Was None
121+
/// }
122+
///
123+
/// fn get_data() -> Contents {
124+
/// Contents::None
125+
/// }
126+
/// ```
110127
pub OPTION_OPTION,
111-
complexity,
128+
pedantic,
112129
"usage of `Option<Option<T>>`"
113130
}
114131

@@ -171,11 +188,35 @@ declare_clippy_lint! {
171188
"a borrow of a boxed type"
172189
}
173190

191+
declare_clippy_lint! {
192+
/// **What it does:** Checks for use of redundant allocations anywhere in the code.
193+
///
194+
/// **Why is this bad?** Expressions such as `Rc<&T>`, `Rc<Rc<T>>`, `Rc<Box<T>>`, `Box<&T>`
195+
/// add an unnecessary level of indirection.
196+
///
197+
/// **Known problems:** None.
198+
///
199+
/// **Example:**
200+
/// ```rust
201+
/// # use std::rc::Rc;
202+
/// fn foo(bar: Rc<&usize>) {}
203+
/// ```
204+
///
205+
/// Better:
206+
///
207+
/// ```rust
208+
/// fn foo(bar: &usize) {}
209+
/// ```
210+
pub REDUNDANT_ALLOCATION,
211+
perf,
212+
"redundant allocation"
213+
}
214+
174215
pub struct Types {
175216
vec_box_size_threshold: u64,
176217
}
177218

178-
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX]);
219+
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION]);
179220

180221
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Types {
181222
fn check_fn(
@@ -217,7 +258,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Types {
217258
}
218259

219260
/// Checks if `qpath` has last segment with type parameter matching `path`
220-
fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath<'_>, path: &[&str]) -> bool {
261+
fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath<'_>, path: &[&str]) -> Option<Span> {
221262
let last = last_path_segment(qpath);
222263
if_chain! {
223264
if let Some(ref params) = last.args;
@@ -230,10 +271,27 @@ fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath<'_>, path: &[&st
230271
if let Some(did) = qpath_res(cx, qpath, ty.hir_id).opt_def_id();
231272
if match_def_path(cx, did, path);
232273
then {
233-
return true;
274+
return Some(ty.span);
234275
}
235276
}
236-
false
277+
None
278+
}
279+
280+
fn match_borrows_parameter(_cx: &LateContext<'_, '_>, qpath: &QPath<'_>) -> Option<Span> {
281+
let last = last_path_segment(qpath);
282+
if_chain! {
283+
if let Some(ref params) = last.args;
284+
if !params.parenthesized;
285+
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
286+
GenericArg::Type(ty) => Some(ty),
287+
_ => None,
288+
});
289+
if let TyKind::Rptr(..) = ty.kind;
290+
then {
291+
return Some(ty.span);
292+
}
293+
}
294+
None
237295
}
238296

239297
impl Types {
@@ -257,6 +315,7 @@ impl Types {
257315
/// The parameter `is_local` distinguishes the context of the type; types from
258316
/// local bindings should only be checked for the `BORROWED_BOX` lint.
259317
#[allow(clippy::too_many_lines)]
318+
#[allow(clippy::cognitive_complexity)]
260319
fn check_ty(&mut self, cx: &LateContext<'_, '_>, hir_ty: &hir::Ty<'_>, is_local: bool) {
261320
if hir_ty.span.from_expansion() {
262321
return;
@@ -267,7 +326,19 @@ impl Types {
267326
let res = qpath_res(cx, qpath, hir_id);
268327
if let Some(def_id) = res.opt_def_id() {
269328
if Some(def_id) == cx.tcx.lang_items().owned_box() {
270-
if match_type_parameter(cx, qpath, &paths::VEC) {
329+
if let Some(span) = match_borrows_parameter(cx, qpath) {
330+
span_lint_and_sugg(
331+
cx,
332+
REDUNDANT_ALLOCATION,
333+
hir_ty.span,
334+
"usage of `Box<&T>`",
335+
"try",
336+
snippet(cx, span, "..").to_string(),
337+
Applicability::MachineApplicable,
338+
);
339+
return; // don't recurse into the type
340+
}
341+
if match_type_parameter(cx, qpath, &paths::VEC).is_some() {
271342
span_lint_and_help(
272343
cx,
273344
BOX_VEC,
@@ -277,6 +348,43 @@ impl Types {
277348
);
278349
return; // don't recurse into the type
279350
}
351+
} else if Some(def_id) == cx.tcx.lang_items().rc() {
352+
if let Some(span) = match_type_parameter(cx, qpath, &paths::RC) {
353+
span_lint_and_sugg(
354+
cx,
355+
REDUNDANT_ALLOCATION,
356+
hir_ty.span,
357+
"usage of `Rc<Rc<T>>`",
358+
"try",
359+
snippet(cx, span, "..").to_string(),
360+
Applicability::MachineApplicable,
361+
);
362+
return; // don't recurse into the type
363+
}
364+
if let Some(span) = match_type_parameter(cx, qpath, &paths::BOX) {
365+
span_lint_and_sugg(
366+
cx,
367+
REDUNDANT_ALLOCATION,
368+
hir_ty.span,
369+
"usage of `Rc<Box<T>>`",
370+
"try",
371+
snippet(cx, span, "..").to_string(),
372+
Applicability::MachineApplicable,
373+
);
374+
return; // don't recurse into the type
375+
}
376+
if let Some(span) = match_borrows_parameter(cx, qpath) {
377+
span_lint_and_sugg(
378+
cx,
379+
REDUNDANT_ALLOCATION,
380+
hir_ty.span,
381+
"usage of `Rc<&T>`",
382+
"try",
383+
snippet(cx, span, "..").to_string(),
384+
Applicability::MachineApplicable,
385+
);
386+
return; // don't recurse into the type
387+
}
280388
} else if cx.tcx.is_diagnostic_item(Symbol::intern("vec_type"), def_id) {
281389
if_chain! {
282390
// Get the _ part of Vec<_>
@@ -314,7 +422,7 @@ impl Types {
314422
}
315423
}
316424
} else if match_def_path(cx, def_id, &paths::OPTION) {
317-
if match_type_parameter(cx, qpath, &paths::OPTION) {
425+
if match_type_parameter(cx, qpath, &paths::OPTION).is_some() {
318426
span_lint(
319427
cx,
320428
OPTION_OPTION,

clippy_lints/src/utils/mod.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,12 @@ use rustc_infer::infer::TyCtxtInferExt;
4141
use rustc_lint::{LateContext, Level, Lint, LintContext};
4242
use rustc_middle::hir::map::Map;
4343
use rustc_middle::traits;
44-
use rustc_middle::ty::{
45-
self,
46-
layout::{self, IntegerExt},
47-
subst::GenericArg,
48-
Binder, Ty, TyCtxt, TypeFoldable,
49-
};
44+
use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Binder, Ty, TyCtxt, TypeFoldable};
5045
use rustc_span::hygiene::{ExpnKind, MacroKind};
5146
use rustc_span::source_map::original_sp;
5247
use rustc_span::symbol::{self, kw, Symbol};
5348
use rustc_span::{BytePos, Pos, Span, DUMMY_SP};
49+
use rustc_target::abi::Integer;
5450
use rustc_trait_selection::traits::predicate_for_trait_def;
5551
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
5652
use rustc_trait_selection::traits::query::normalize::AtExt;
@@ -1080,9 +1076,7 @@ pub fn get_arg_name(pat: &Pat<'_>) -> Option<ast::Name> {
10801076
}
10811077

10821078
pub fn int_bits(tcx: TyCtxt<'_>, ity: ast::IntTy) -> u64 {
1083-
layout::Integer::from_attr(&tcx, attr::IntType::SignedInt(ity))
1084-
.size()
1085-
.bits()
1079+
Integer::from_attr(&tcx, attr::IntType::SignedInt(ity)).size().bits()
10861080
}
10871081

10881082
#[allow(clippy::cast_possible_wrap)]
@@ -1101,9 +1095,7 @@ pub fn unsext(tcx: TyCtxt<'_>, u: i128, ity: ast::IntTy) -> u128 {
11011095

11021096
/// clip unused bytes
11031097
pub fn clip(tcx: TyCtxt<'_>, u: u128, ity: ast::UintTy) -> u128 {
1104-
let bits = layout::Integer::from_attr(&tcx, attr::IntType::UnsignedInt(ity))
1105-
.size()
1106-
.bits();
1098+
let bits = Integer::from_attr(&tcx, attr::IntType::UnsignedInt(ity)).size().bits();
11071099
let amt = 128 - bits;
11081100
(u << amt) >> amt
11091101
}

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub const BEGIN_PANIC: [&str; 3] = ["std", "panicking", "begin_panic"];
1010
pub const BEGIN_PANIC_FMT: [&str; 3] = ["std", "panicking", "begin_panic_fmt"];
1111
pub const BINARY_HEAP: [&str; 4] = ["alloc", "collections", "binary_heap", "BinaryHeap"];
1212
pub const BORROW_TRAIT: [&str; 3] = ["core", "borrow", "Borrow"];
13+
pub const BOX: [&str; 3] = ["alloc", "boxed", "Box"];
1314
pub const BTREEMAP: [&str; 5] = ["alloc", "collections", "btree", "map", "BTreeMap"];
1415
pub const BTREEMAP_ENTRY: [&str; 5] = ["alloc", "collections", "btree", "map", "Entry"];
1516
pub const BTREESET: [&str; 5] = ["alloc", "collections", "btree", "set", "BTreeSet"];

0 commit comments

Comments
 (0)