Skip to content

Commit cd47064

Browse files
committed
Run cargo dev fmt
1 parent 798a5cf commit cd47064

File tree

3 files changed

+71
-116
lines changed

3 files changed

+71
-116
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use crate::utils::qualify_min_const_fn::is_min_const_fn;
12
use crate::utils::{fn_has_unsatisfiable_preds, has_drop, is_entrypoint_fn, span_lint, trait_ref_of_method};
23
use rustc_hir as hir;
34
use rustc_hir::intravisit::FnKind;
45
use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId};
56
use rustc_lint::{LateContext, LateLintPass};
67
use rustc_middle::lint::in_external_macro;
7-
use crate::utils::qualify_min_const_fn::is_min_const_fn;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::Span;
1010
use rustc_typeck::hir_ty_to_ty;

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub mod internal_lints;
1818
pub mod numeric_literal;
1919
pub mod paths;
2020
pub mod ptr;
21+
pub mod qualify_min_const_fn;
2122
pub mod sugg;
2223
pub mod usage;
23-
pub mod qualify_min_const_fn;
2424

2525
pub use self::attrs::*;
2626
pub use self::diagnostics::*;

clippy_lints/src/utils/qualify_min_const_fn.rs

Lines changed: 69 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::DefId;
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::subst::GenericArgKind;
55
use rustc_middle::ty::{self, adjustment::PointerCast, Ty, TyCtxt};
6-
use rustc_span::symbol::{sym};
6+
use rustc_span::symbol::sym;
77
use rustc_span::Span;
88
use rustc_target::spec::abi::Abi::RustIntrinsic;
99
use std::borrow::Cow;
@@ -23,15 +23,9 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -
2323
| ty::PredicateAtom::ConstEvaluatable(..)
2424
| ty::PredicateAtom::ConstEquate(..)
2525
| ty::PredicateAtom::TypeWellFormedFromEnv(..) => continue,
26-
ty::PredicateAtom::ObjectSafe(_) => {
27-
panic!("object safe predicate on function: {:#?}", predicate)
28-
}
29-
ty::PredicateAtom::ClosureKind(..) => {
30-
panic!("closure kind predicate on function: {:#?}", predicate)
31-
}
32-
ty::PredicateAtom::Subtype(_) => {
33-
panic!("subtype predicate on function: {:#?}", predicate)
34-
}
26+
ty::PredicateAtom::ObjectSafe(_) => panic!("object safe predicate on function: {:#?}", predicate),
27+
ty::PredicateAtom::ClosureKind(..) => panic!("closure kind predicate on function: {:#?}", predicate),
28+
ty::PredicateAtom::Subtype(_) => panic!("subtype predicate on function: {:#?}", predicate),
3529
ty::PredicateAtom::Trait(pred, _) => {
3630
if Some(pred.def_id()) == tcx.lang_items().sized_trait() {
3731
continue;
@@ -47,12 +41,12 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -
4741
on const fn parameters are unstable"
4842
.into(),
4943
));
50-
}
44+
},
5145
// other kinds of bounds are either tautologies
5246
// or cause errors in other passes
5347
_ => continue,
5448
}
55-
}
49+
},
5650
}
5751
}
5852
match predicates.parent {
@@ -92,24 +86,23 @@ fn check_ty(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
9286

9387
match ty.kind() {
9488
ty::Ref(_, _, hir::Mutability::Mut) => {
95-
return Err((span, "mutable references in const fn are unstable".into()));
96-
}
89+
return Err((span, "mutable references in const fn are unstable".into()));
90+
},
9791
ty::Opaque(..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
9892
ty::FnPtr(..) => {
99-
return Err((span, "function pointers in const fn are unstable".into()));
100-
}
93+
return Err((span, "function pointers in const fn are unstable".into()));
94+
},
10195
ty::Dynamic(preds, _) => {
10296
for pred in preds.iter() {
10397
match pred.skip_binder() {
104-
ty::ExistentialPredicate::AutoTrait(_)
105-
| ty::ExistentialPredicate::Projection(_) => {
98+
ty::ExistentialPredicate::AutoTrait(_) | ty::ExistentialPredicate::Projection(_) => {
10699
return Err((
107100
span,
108101
"trait bounds other than `Sized` \
109102
on const fn parameters are unstable"
110103
.into(),
111104
));
112-
}
105+
},
113106
ty::ExistentialPredicate::Trait(trait_ref) => {
114107
if Some(trait_ref.def_id) != tcx.lang_items().sized_trait() {
115108
return Err((
@@ -119,55 +112,40 @@ fn check_ty(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
119112
.into(),
120113
));
121114
}
122-
}
115+
},
123116
}
124117
}
125-
}
126-
_ => {}
118+
},
119+
_ => {},
127120
}
128121
}
129122
Ok(())
130123
}
131124

132-
fn check_rvalue(
133-
tcx: TyCtxt<'tcx>,
134-
body: &Body<'tcx>,
135-
def_id: DefId,
136-
rvalue: &Rvalue<'tcx>,
137-
span: Span,
138-
) -> McfResult {
125+
fn check_rvalue(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId, rvalue: &Rvalue<'tcx>, span: Span) -> McfResult {
139126
match rvalue {
140-
Rvalue::ThreadLocalRef(_) => {
141-
Err((span, "cannot access thread local storage in const fn".into()))
142-
}
143-
Rvalue::Repeat(operand, _) | Rvalue::Use(operand) => {
144-
check_operand(tcx, operand, span, body)
145-
}
146-
Rvalue::Len(place)
147-
| Rvalue::Discriminant(place)
148-
| Rvalue::Ref(_, _, place)
149-
| Rvalue::AddressOf(_, place) => check_place(tcx, *place, span, body),
127+
Rvalue::ThreadLocalRef(_) => Err((span, "cannot access thread local storage in const fn".into())),
128+
Rvalue::Repeat(operand, _) | Rvalue::Use(operand) => check_operand(tcx, operand, span, body),
129+
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => {
130+
check_place(tcx, *place, span, body)
131+
},
150132
Rvalue::Cast(CastKind::Misc, operand, cast_ty) => {
151133
use rustc_middle::ty::cast::CastTy;
152134
let cast_in = CastTy::from_ty(operand.ty(body, tcx)).expect("bad input type for cast");
153135
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
154136
match (cast_in, cast_out) {
155137
(CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Int(_)) => {
156138
Err((span, "casting pointers to ints is unstable in const fn".into()))
157-
}
139+
},
158140
_ => check_operand(tcx, operand, span, body),
159141
}
160-
}
161-
Rvalue::Cast(
162-
CastKind::Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer),
163-
operand,
164-
_,
165-
) => check_operand(tcx, operand, span, body),
142+
},
143+
Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer), operand, _) => {
144+
check_operand(tcx, operand, span, body)
145+
},
166146
Rvalue::Cast(
167147
CastKind::Pointer(
168-
PointerCast::UnsafeFnPointer
169-
| PointerCast::ClosureFnPointer(_)
170-
| PointerCast::ReifyFnPointer,
148+
PointerCast::UnsafeFnPointer | PointerCast::ClosureFnPointer(_) | PointerCast::ReifyFnPointer,
171149
),
172150
_,
173151
_,
@@ -177,10 +155,7 @@ fn check_rvalue(
177155
deref_ty.ty
178156
} else {
179157
// We cannot allow this for now.
180-
return Err((
181-
span,
182-
"unsizing casts are only allowed for references right now".into(),
183-
));
158+
return Err((span, "unsizing casts are only allowed for references right now".into()));
184159
};
185160
let unsized_ty = tcx.struct_tail_erasing_lifetimes(pointee_ty, tcx.param_env(def_id));
186161
if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
@@ -191,7 +166,7 @@ fn check_rvalue(
191166
// We just can't allow trait objects until we have figured out trait method calls.
192167
Err((span, "unsizing casts are not allowed in const fn".into()))
193168
}
194-
}
169+
},
195170
// binops are fine on integers
196171
Rvalue::BinaryOp(_, lhs, rhs) | Rvalue::CheckedBinaryOp(_, lhs, rhs) => {
197172
check_operand(tcx, lhs, span, body)?;
@@ -200,53 +175,45 @@ fn check_rvalue(
200175
if ty.is_integral() || ty.is_bool() || ty.is_char() {
201176
Ok(())
202177
} else {
203-
Err((span, "only int, `bool` and `char` operations are stable in const fn".into()))
178+
Err((
179+
span,
180+
"only int, `bool` and `char` operations are stable in const fn".into(),
181+
))
204182
}
205-
}
183+
},
206184
Rvalue::NullaryOp(NullOp::SizeOf, _) => Ok(()),
207-
Rvalue::NullaryOp(NullOp::Box, _) => {
208-
Err((span, "heap allocations are not allowed in const fn".into()))
209-
}
185+
Rvalue::NullaryOp(NullOp::Box, _) => Err((span, "heap allocations are not allowed in const fn".into())),
210186
Rvalue::UnaryOp(_, operand) => {
211187
let ty = operand.ty(body, tcx);
212188
if ty.is_integral() || ty.is_bool() {
213189
check_operand(tcx, operand, span, body)
214190
} else {
215191
Err((span, "only int and `bool` operations are stable in const fn".into()))
216192
}
217-
}
193+
},
218194
Rvalue::Aggregate(_, operands) => {
219195
for operand in operands {
220196
check_operand(tcx, operand, span, body)?;
221197
}
222198
Ok(())
223-
}
199+
},
224200
}
225201
}
226202

227-
fn check_statement(
228-
tcx: TyCtxt<'tcx>,
229-
body: &Body<'tcx>,
230-
def_id: DefId,
231-
statement: &Statement<'tcx>,
232-
) -> McfResult {
203+
fn check_statement(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId, statement: &Statement<'tcx>) -> McfResult {
233204
let span = statement.source_info.span;
234205
match &statement.kind {
235206
StatementKind::Assign(box (place, rval)) => {
236-
check_place(tcx, *place, span, body)?;
207+
check_place(tcx, *place, span, body)?;
237208
check_rvalue(tcx, body, def_id, rval, span)
238-
}
209+
},
239210

240211
StatementKind::FakeRead(_, place) => check_place(tcx, **place, span, body),
241212

242213
// just an assignment
243-
StatementKind::SetDiscriminant { place, .. } => {
244-
check_place(tcx, **place, span, body)
245-
}
214+
StatementKind::SetDiscriminant { place, .. } => check_place(tcx, **place, span, body),
246215

247-
StatementKind::LlvmInlineAsm { .. } => {
248-
Err((span, "cannot use inline assembly in const fn".into()))
249-
}
216+
StatementKind::LlvmInlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
250217

251218
// These are all NOPs
252219
StatementKind::StorageLive(_)
@@ -258,12 +225,7 @@ fn check_statement(
258225
}
259226
}
260227

261-
fn check_operand(
262-
tcx: TyCtxt<'tcx>,
263-
operand: &Operand<'tcx>,
264-
span: Span,
265-
body: &Body<'tcx>,
266-
) -> McfResult {
228+
fn check_operand(tcx: TyCtxt<'tcx>, operand: &Operand<'tcx>, span: Span, body: &Body<'tcx>) -> McfResult {
267229
match operand {
268230
Operand::Move(place) | Operand::Copy(place) => check_place(tcx, *place, span, body),
269231
Operand::Constant(c) => match c.check_static_ptr(tcx) {
@@ -273,12 +235,7 @@ fn check_operand(
273235
}
274236
}
275237

276-
fn check_place(
277-
tcx: TyCtxt<'tcx>,
278-
place: Place<'tcx>,
279-
span: Span,
280-
body: &Body<'tcx>,
281-
) -> McfResult {
238+
fn check_place(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &Body<'tcx>) -> McfResult {
282239
let mut cursor = place.projection.as_ref();
283240
while let &[ref proj_base @ .., elem] = cursor {
284241
cursor = proj_base;
@@ -288,26 +245,22 @@ fn check_place(
288245
if let Some(def) = base_ty.ty_adt_def() {
289246
// No union field accesses in `const fn`
290247
if def.is_union() {
291-
return Err((span, "accessing union fields is unstable".into()));
248+
return Err((span, "accessing union fields is unstable".into()));
292249
}
293250
}
294-
}
251+
},
295252
ProjectionElem::ConstantIndex { .. }
296253
| ProjectionElem::Downcast(..)
297254
| ProjectionElem::Subslice { .. }
298255
| ProjectionElem::Deref
299-
| ProjectionElem::Index(_) => {}
256+
| ProjectionElem::Index(_) => {},
300257
}
301258
}
302259

303260
Ok(())
304261
}
305262

306-
fn check_terminator(
307-
tcx: TyCtxt<'tcx>,
308-
body: &'a Body<'tcx>,
309-
terminator: &Terminator<'tcx>,
310-
) -> McfResult {
263+
fn check_terminator(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, terminator: &Terminator<'tcx>) -> McfResult {
311264
let span = terminator.source_info.span;
312265
match &terminator.kind {
313266
TerminatorKind::FalseEdge { .. }
@@ -317,20 +270,23 @@ fn check_terminator(
317270
| TerminatorKind::Resume
318271
| TerminatorKind::Unreachable => Ok(()),
319272

320-
TerminatorKind::Drop { place, .. } => check_place(tcx, *place, span, body),
273+
TerminatorKind::Drop { place, .. } => check_place(tcx, *place, span, body),
321274
TerminatorKind::DropAndReplace { place, value, .. } => {
322-
check_place(tcx, *place, span, body)?;
275+
check_place(tcx, *place, span, body)?;
323276
check_operand(tcx, value, span, body)
324-
}
277+
},
325278

326-
TerminatorKind::SwitchInt { discr, switch_ty: _, values: _, targets: _ } => {
327-
check_operand(tcx, discr, span, body)
328-
}
279+
TerminatorKind::SwitchInt {
280+
discr,
281+
switch_ty: _,
282+
values: _,
283+
targets: _,
284+
} => check_operand(tcx, discr, span, body),
329285

330286
TerminatorKind::Abort => Err((span, "abort is not stable in const fn".into())),
331287
TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => {
332288
Err((span, "const fn generators are unstable".into()))
333-
}
289+
},
334290

335291
TerminatorKind::Call {
336292
func,
@@ -342,8 +298,7 @@ fn check_terminator(
342298
} => {
343299
let fn_ty = func.ty(body, tcx);
344300
if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() {
345-
if !rustc_mir::const_eval::is_min_const_fn(tcx, fn_def_id)
346-
{
301+
if !rustc_mir::const_eval::is_min_const_fn(tcx, fn_def_id) {
347302
return Err((
348303
span,
349304
format!(
@@ -359,9 +314,7 @@ fn check_terminator(
359314
// within const fns. `transmute` is allowed in all other const contexts.
360315
// This won't really scale to more intrinsics or functions. Let's allow const
361316
// transmutes in const fn before we add more hacks to this.
362-
if tcx.fn_sig(fn_def_id).abi() == RustIntrinsic
363-
&& tcx.item_name(fn_def_id) == sym::transmute
364-
{
317+
if tcx.fn_sig(fn_def_id).abi() == RustIntrinsic && tcx.item_name(fn_def_id) == sym::transmute {
365318
return Err((
366319
span,
367320
"can only call `transmute` from const items, not `const fn`".into(),
@@ -377,14 +330,16 @@ fn check_terminator(
377330
} else {
378331
Err((span, "can only call other const fns within const fn".into()))
379332
}
380-
}
333+
},
381334

382-
TerminatorKind::Assert { cond, expected: _, msg: _, target: _, cleanup: _ } => {
383-
check_operand(tcx, cond, span, body)
384-
}
335+
TerminatorKind::Assert {
336+
cond,
337+
expected: _,
338+
msg: _,
339+
target: _,
340+
cleanup: _,
341+
} => check_operand(tcx, cond, span, body),
385342

386-
TerminatorKind::InlineAsm { .. } => {
387-
Err((span, "cannot use inline assembly in const fn".into()))
388-
}
343+
TerminatorKind::InlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
389344
}
390345
}

0 commit comments

Comments
 (0)