Skip to content

Commit a039217

Browse files
committed
Auto merge of #69796 - Centril:rollup-xg85jmx, r=Centril
Rollup of 9 pull requests Successful merges: - #67741 (When encountering an Item in a pat context, point at the item def) - #68985 (Parse & reject postfix operators after casts) - #69656 (Use .next() instead of .nth(0) on iterators.) - #69680 (rustc_expand: Factor out `Annotatable::into_tokens` to a separate method) - #69690 (test(pattern): add tests for combinations of pattern features) - #69706 (Use subslice patterns in slice methods) - #69727 (Avoid using `unwrap()` in suggestions) - #69754 (Update deprecation version to 1.42 for Error::description) - #69782 (Don't redundantly repeat field names (clippy::redundant_field_names)) Failed merges: r? @ghost
2 parents 2890b37 + 709325a commit a039217

File tree

102 files changed

+1311
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1311
-215
lines changed

src/liballoc/collections/linked_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ impl<T> LinkedList<T> {
959959
let it = self.head;
960960
let old_len = self.len;
961961

962-
DrainFilter { list: self, it: it, pred: filter, idx: 0, old_len: old_len }
962+
DrainFilter { list: self, it, pred: filter, idx: 0, old_len }
963963
}
964964
}
965965

src/liballoc/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ struct SetLenOnDrop<'a> {
16591659
impl<'a> SetLenOnDrop<'a> {
16601660
#[inline]
16611661
fn new(len: &'a mut usize) -> Self {
1662-
SetLenOnDrop { local_len: *len, len: len }
1662+
SetLenOnDrop { local_len: *len, len }
16631663
}
16641664

16651665
#[inline]

src/libcore/slice/mod.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<T> [T] {
103103
#[stable(feature = "rust1", since = "1.0.0")]
104104
#[inline]
105105
pub fn first(&self) -> Option<&T> {
106-
self.get(0)
106+
if let [first, ..] = self { Some(first) } else { None }
107107
}
108108

109109
/// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
@@ -121,7 +121,7 @@ impl<T> [T] {
121121
#[stable(feature = "rust1", since = "1.0.0")]
122122
#[inline]
123123
pub fn first_mut(&mut self) -> Option<&mut T> {
124-
self.get_mut(0)
124+
if let [first, ..] = self { Some(first) } else { None }
125125
}
126126

127127
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -139,7 +139,7 @@ impl<T> [T] {
139139
#[stable(feature = "slice_splits", since = "1.5.0")]
140140
#[inline]
141141
pub fn split_first(&self) -> Option<(&T, &[T])> {
142-
if self.is_empty() { None } else { Some((&self[0], &self[1..])) }
142+
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
143143
}
144144

145145
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -159,12 +159,7 @@ impl<T> [T] {
159159
#[stable(feature = "slice_splits", since = "1.5.0")]
160160
#[inline]
161161
pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
162-
if self.is_empty() {
163-
None
164-
} else {
165-
let split = self.split_at_mut(1);
166-
Some((&mut split.0[0], split.1))
167-
}
162+
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
168163
}
169164

170165
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -182,8 +177,7 @@ impl<T> [T] {
182177
#[stable(feature = "slice_splits", since = "1.5.0")]
183178
#[inline]
184179
pub fn split_last(&self) -> Option<(&T, &[T])> {
185-
let len = self.len();
186-
if len == 0 { None } else { Some((&self[len - 1], &self[..(len - 1)])) }
180+
if let [init @ .., last] = self { Some((last, init)) } else { None }
187181
}
188182

189183
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -203,13 +197,7 @@ impl<T> [T] {
203197
#[stable(feature = "slice_splits", since = "1.5.0")]
204198
#[inline]
205199
pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
206-
let len = self.len();
207-
if len == 0 {
208-
None
209-
} else {
210-
let split = self.split_at_mut(len - 1);
211-
Some((&mut split.1[0], split.0))
212-
}
200+
if let [init @ .., last] = self { Some((last, init)) } else { None }
213201
}
214202

215203
/// Returns the last element of the slice, or `None` if it is empty.
@@ -226,8 +214,7 @@ impl<T> [T] {
226214
#[stable(feature = "rust1", since = "1.0.0")]
227215
#[inline]
228216
pub fn last(&self) -> Option<&T> {
229-
let last_idx = self.len().checked_sub(1)?;
230-
self.get(last_idx)
217+
if let [.., last] = self { Some(last) } else { None }
231218
}
232219

233220
/// Returns a mutable pointer to the last item in the slice.
@@ -245,8 +232,7 @@ impl<T> [T] {
245232
#[stable(feature = "rust1", since = "1.0.0")]
246233
#[inline]
247234
pub fn last_mut(&mut self) -> Option<&mut T> {
248-
let last_idx = self.len().checked_sub(1)?;
249-
self.get_mut(last_idx)
235+
if let [.., last] = self { Some(last) } else { None }
250236
}
251237

252238
/// Returns a reference to an element or subslice depending on the type of

src/libproc_macro/diagnostic.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ pub struct Diagnostic {
5555
}
5656

5757
macro_rules! diagnostic_child_methods {
58-
($spanned:ident, $regular:ident, $level:expr) => (
58+
($spanned:ident, $regular:ident, $level:expr) => {
5959
/// Adds a new child diagnostic message to `self` with the level
6060
/// identified by this method's name with the given `spans` and
6161
/// `message`.
6262
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
6363
pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
64-
where S: MultiSpan, T: Into<String>
64+
where
65+
S: MultiSpan,
66+
T: Into<String>,
6567
{
6668
self.children.push(Diagnostic::spanned(spans, $level, message));
6769
self
@@ -74,7 +76,7 @@ macro_rules! diagnostic_child_methods {
7476
self.children.push(Diagnostic::new($level, message));
7577
self
7678
}
77-
)
79+
};
7880
}
7981

8082
/// Iterator over the children diagnostics of a `Diagnostic`.
@@ -96,7 +98,7 @@ impl Diagnostic {
9698
/// Creates a new diagnostic with the given `level` and `message`.
9799
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
98100
pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
99-
Diagnostic { level: level, message: message.into(), spans: vec![], children: vec![] }
101+
Diagnostic { level, message: message.into(), spans: vec![], children: vec![] }
100102
}
101103

102104
/// Creates a new diagnostic with the given `level` and `message` pointing to
@@ -107,12 +109,7 @@ impl Diagnostic {
107109
S: MultiSpan,
108110
T: Into<String>,
109111
{
110-
Diagnostic {
111-
level: level,
112-
message: message.into(),
113-
spans: spans.into_spans(),
114-
children: vec![],
115-
}
112+
Diagnostic { level, message: message.into(), spans: spans.into_spans(), children: vec![] }
116113
}
117114

118115
diagnostic_child_methods!(span_error, error, Level::Error);

src/librustc/hir/map/definitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl DefPath {
192192
}
193193
}
194194
data.reverse();
195-
DefPath { data: data, krate: krate }
195+
DefPath { data, krate }
196196
}
197197

198198
/// Returns a string representation of the `DefPath` without

src/librustc/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ impl<'tcx> Debug for TerminatorKind<'tcx> {
14461446
match successor_count {
14471447
0 => Ok(()),
14481448

1449-
1 => write!(fmt, " -> {:?}", self.successors().nth(0).unwrap()),
1449+
1 => write!(fmt, " -> {:?}", self.successors().next().unwrap()),
14501450

14511451
_ => {
14521452
write!(fmt, " -> [")?;

src/librustc/mir/mono.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub enum Visibility {
258258

259259
impl<'tcx> CodegenUnit<'tcx> {
260260
pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
261-
CodegenUnit { name: name, items: Default::default(), size_estimate: None }
261+
CodegenUnit { name, items: Default::default(), size_estimate: None }
262262
}
263263

264264
pub fn name(&self) -> Symbol {

src/librustc/traits/structural_impls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ impl<'a, 'tcx> Lift<'tcx> for traits::Vtable<'a, ()> {
532532
nested,
533533
}) => tcx.lift(&substs).map(|substs| {
534534
traits::VtableGenerator(traits::VtableGeneratorData {
535-
generator_def_id: generator_def_id,
536-
substs: substs,
537-
nested: nested,
535+
generator_def_id,
536+
substs,
537+
nested,
538538
})
539539
}),
540540
traits::VtableClosure(traits::VtableClosureData { closure_def_id, substs, nested }) => {

src/librustc/ty/context.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2256,22 +2256,22 @@ impl<'tcx> TyCtxt<'tcx> {
22562256

22572257
#[inline]
22582258
pub fn mk_mut_ref(self, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
2259-
self.mk_ref(r, TypeAndMut { ty: ty, mutbl: hir::Mutability::Mut })
2259+
self.mk_ref(r, TypeAndMut { ty, mutbl: hir::Mutability::Mut })
22602260
}
22612261

22622262
#[inline]
22632263
pub fn mk_imm_ref(self, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
2264-
self.mk_ref(r, TypeAndMut { ty: ty, mutbl: hir::Mutability::Not })
2264+
self.mk_ref(r, TypeAndMut { ty, mutbl: hir::Mutability::Not })
22652265
}
22662266

22672267
#[inline]
22682268
pub fn mk_mut_ptr(self, ty: Ty<'tcx>) -> Ty<'tcx> {
2269-
self.mk_ptr(TypeAndMut { ty: ty, mutbl: hir::Mutability::Mut })
2269+
self.mk_ptr(TypeAndMut { ty, mutbl: hir::Mutability::Mut })
22702270
}
22712271

22722272
#[inline]
22732273
pub fn mk_imm_ptr(self, ty: Ty<'tcx>) -> Ty<'tcx> {
2274-
self.mk_ptr(TypeAndMut { ty: ty, mutbl: hir::Mutability::Not })
2274+
self.mk_ptr(TypeAndMut { ty, mutbl: hir::Mutability::Not })
22752275
}
22762276

22772277
#[inline]
@@ -2393,7 +2393,7 @@ impl<'tcx> TyCtxt<'tcx> {
23932393

23942394
#[inline]
23952395
pub fn mk_ty_param(self, index: u32, name: Symbol) -> Ty<'tcx> {
2396-
self.mk_ty(Param(ParamTy { index, name: name }))
2396+
self.mk_ty(Param(ParamTy { index, name }))
23972397
}
23982398

23992399
#[inline]

src/librustc/ty/instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl<'tcx> Instance<'tcx> {
241241
def_id,
242242
substs
243243
);
244-
Instance { def: InstanceDef::Item(def_id), substs: substs }
244+
Instance { def: InstanceDef::Item(def_id), substs }
245245
}
246246

247247
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub trait DefIdTree: Copy {
370370

371371
impl<'tcx> DefIdTree for TyCtxt<'tcx> {
372372
fn parent(self, id: DefId) -> Option<DefId> {
373-
self.def_key(id).parent.map(|index| DefId { index: index, ..id })
373+
self.def_key(id).parent.map(|index| DefId { index, ..id })
374374
}
375375
}
376376

@@ -2227,7 +2227,7 @@ impl ReprOptions {
22272227
if !tcx.consider_optimizing(|| format!("Reorder fields of {:?}", tcx.def_path_str(did))) {
22282228
flags.insert(ReprFlags::IS_LINEAR);
22292229
}
2230-
ReprOptions { int: size, align: max_align, pack: min_pack, flags: flags }
2230+
ReprOptions { int: size, align: max_align, pack: min_pack, flags }
22312231
}
22322232

22332233
#[inline]

src/librustc/ty/normalize_erasing_regions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ impl<'tcx> TyCtxt<'tcx> {
3434
if !value.has_projections() {
3535
value
3636
} else {
37-
value.fold_with(&mut NormalizeAfterErasingRegionsFolder {
38-
tcx: self,
39-
param_env: param_env,
40-
})
37+
value.fold_with(&mut NormalizeAfterErasingRegionsFolder { tcx: self, param_env })
4138
}
4239
}
4340

src/librustc/ty/relate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
287287
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
288288
} else {
289289
let substs = relate_substs(relation, None, a.substs, b.substs)?;
290-
Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
290+
Ok(ty::TraitRef { def_id: a.def_id, substs })
291291
}
292292
}
293293
}
@@ -303,7 +303,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
303303
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
304304
} else {
305305
let substs = relate_substs(relation, None, a.substs, b.substs)?;
306-
Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
306+
Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs })
307307
}
308308
}
309309
}

src/librustc/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ pub struct ParamTy {
11931193

11941194
impl<'tcx> ParamTy {
11951195
pub fn new(index: u32, name: Symbol) -> ParamTy {
1196-
ParamTy { index, name: name }
1196+
ParamTy { index, name }
11971197
}
11981198

11991199
pub fn for_self() -> ParamTy {

src/librustc/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl<'tcx> TyCtxt<'tcx> {
357357
let mut dtor_did = None;
358358
let ty = self.type_of(adt_did);
359359
self.for_each_relevant_impl(drop_trait, ty, |impl_did| {
360-
if let Some(item) = self.associated_items(impl_did).in_definition_order().nth(0) {
360+
if let Some(item) = self.associated_items(impl_did).in_definition_order().next() {
361361
if validate(self, impl_did).is_ok() {
362362
dtor_did = Some(item.def_id);
363363
}

src/librustc_builtin_macros/deriving/generic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl<'a> TraitDef<'a> {
482482
})
483483
.cloned(),
484484
);
485-
push(Annotatable::Item(P(ast::Item { attrs: attrs, ..(*newitem).clone() })))
485+
push(Annotatable::Item(P(ast::Item { attrs, ..(*newitem).clone() })))
486486
}
487487
_ => {
488488
// Non-Item derive is an error, but it should have been

src/librustc_codegen_llvm/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl LlvmType for CastTarget {
148148
.prefix
149149
.iter()
150150
.flat_map(|option_kind| {
151-
option_kind.map(|kind| Reg { kind: kind, size: self.prefix_chunk }.llvm_type(cx))
151+
option_kind.map(|kind| Reg { kind, size: self.prefix_chunk }.llvm_type(cx))
152152
})
153153
.chain((0..rest_count).map(|_| rest_ll_unit))
154154
.collect();

src/librustc_expand/base.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::ast::{self, Attribute, Name, NodeId, PatKind};
44
use rustc_ast::mut_visit::{self, MutVisitor};
55
use rustc_ast::ptr::P;
66
use rustc_ast::token;
7-
use rustc_ast::tokenstream::{self, TokenStream};
7+
use rustc_ast::tokenstream::{self, TokenStream, TokenTree};
88
use rustc_ast::visit::{AssocCtxt, Visitor};
99
use rustc_attr::{self as attr, Deprecation, HasAttrs, Stability};
1010
use rustc_data_structures::fx::FxHashMap;
@@ -118,6 +118,31 @@ impl Annotatable {
118118
}
119119
}
120120

121+
crate fn into_tokens(self) -> TokenStream {
122+
// `Annotatable` can be converted into tokens directly, but we
123+
// are packing it into a nonterminal as a piece of AST to make
124+
// the produced token stream look nicer in pretty-printed form.
125+
let nt = match self {
126+
Annotatable::Item(item) => token::NtItem(item),
127+
Annotatable::TraitItem(item) | Annotatable::ImplItem(item) => {
128+
token::NtItem(P(item.and_then(ast::AssocItem::into_item)))
129+
}
130+
Annotatable::ForeignItem(item) => {
131+
token::NtItem(P(item.and_then(ast::ForeignItem::into_item)))
132+
}
133+
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
134+
Annotatable::Expr(expr) => token::NtExpr(expr),
135+
Annotatable::Arm(..)
136+
| Annotatable::Field(..)
137+
| Annotatable::FieldPat(..)
138+
| Annotatable::GenericParam(..)
139+
| Annotatable::Param(..)
140+
| Annotatable::StructField(..)
141+
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
142+
};
143+
TokenTree::token(token::Interpolated(Lrc::new(nt)), DUMMY_SP).into()
144+
}
145+
121146
pub fn expect_item(self) -> P<ast::Item> {
122147
match self {
123148
Annotatable::Item(i) => i,

0 commit comments

Comments
 (0)