23
23
24
24
use crate :: { EarlyContext , EarlyLintPass , LateContext , LateLintPass , LintContext } ;
25
25
use rustc:: hir:: map:: Map ;
26
+ use rustc:: lint:: LintDiagnosticBuilder ;
26
27
use rustc:: traits:: misc:: can_type_implement_copy;
27
28
use rustc:: ty:: { self , layout:: VariantIdx , Ty , TyCtxt } ;
28
29
use rustc_ast_pretty:: pprust:: { self , expr_to_string} ;
29
30
use rustc_data_structures:: fx:: FxHashSet ;
30
31
use rustc_errors:: { Applicability , DiagnosticBuilder } ;
31
- use rustc:: lint:: LintDiagnosticBuilder ;
32
32
use rustc_feature:: Stability ;
33
33
use rustc_feature:: { deprecated_attributes, AttributeGate , AttributeTemplate , AttributeType } ;
34
34
use rustc_hir as hir;
@@ -107,7 +107,9 @@ impl BoxPointers {
107
107
fn check_heap_type ( & self , cx : & LateContext < ' _ , ' _ > , span : Span , ty : Ty < ' _ > ) {
108
108
for leaf_ty in ty. walk ( ) {
109
109
if leaf_ty. is_box ( ) {
110
- cx. struct_span_lint ( BOX_POINTERS , span, |lint| lint. build ( & format ! ( "type uses owned (Box type) pointers: {}" , ty) ) . emit ( ) ) ;
110
+ cx. struct_span_lint ( BOX_POINTERS , span, |lint| {
111
+ lint. build ( & format ! ( "type uses owned (Box type) pointers: {}" , ty) ) . emit ( )
112
+ } ) ;
111
113
}
112
114
}
113
115
}
@@ -214,7 +216,12 @@ declare_lint! {
214
216
declare_lint_pass ! ( UnsafeCode => [ UNSAFE_CODE ] ) ;
215
217
216
218
impl UnsafeCode {
217
- fn report_unsafe ( & self , cx : & EarlyContext < ' _ > , span : Span , decorate : impl for < ' a > FnOnce ( LintDiagnosticBuilder < ' a > ) ) {
219
+ fn report_unsafe (
220
+ & self ,
221
+ cx : & EarlyContext < ' _ > ,
222
+ span : Span ,
223
+ decorate : impl for < ' a > FnOnce ( LintDiagnosticBuilder < ' a > ) ,
224
+ ) {
218
225
// This comes from a macro that has `#[allow_internal_unsafe]`.
219
226
if span. allows_unsafe ( ) {
220
227
return ;
@@ -227,33 +234,40 @@ impl UnsafeCode {
227
234
impl EarlyLintPass for UnsafeCode {
228
235
fn check_attribute ( & mut self , cx : & EarlyContext < ' _ > , attr : & ast:: Attribute ) {
229
236
if attr. check_name ( sym:: allow_internal_unsafe) {
230
- self . report_unsafe (
231
- cx,
232
- attr. span ,
233
- |lint| lint. build ( "`allow_internal_unsafe` allows defining \
237
+ self . report_unsafe ( cx, attr. span , |lint| {
238
+ lint. build (
239
+ "`allow_internal_unsafe` allows defining \
234
240
macros using unsafe without triggering \
235
- the `unsafe_code` lint at their call site") . emit ( ) ,
236
- ) ;
241
+ the `unsafe_code` lint at their call site",
242
+ )
243
+ . emit ( )
244
+ } ) ;
237
245
}
238
246
}
239
247
240
248
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
241
249
if let ast:: ExprKind :: Block ( ref blk, _) = e. kind {
242
250
// Don't warn about generated blocks; that'll just pollute the output.
243
251
if blk. rules == ast:: BlockCheckMode :: Unsafe ( ast:: UserProvided ) {
244
- self . report_unsafe ( cx, blk. span , |lint| lint. build ( "usage of an `unsafe` block" ) . emit ( ) ) ;
252
+ self . report_unsafe ( cx, blk. span , |lint| {
253
+ lint. build ( "usage of an `unsafe` block" ) . emit ( )
254
+ } ) ;
245
255
}
246
256
}
247
257
}
248
258
249
259
fn check_item ( & mut self , cx : & EarlyContext < ' _ > , it : & ast:: Item ) {
250
260
match it. kind {
251
261
ast:: ItemKind :: Trait ( _, ast:: Unsafety :: Unsafe , ..) => {
252
- self . report_unsafe ( cx, it. span , |lint| lint. build ( "declaration of an `unsafe` trait" ) . emit ( ) )
262
+ self . report_unsafe ( cx, it. span , |lint| {
263
+ lint. build ( "declaration of an `unsafe` trait" ) . emit ( )
264
+ } )
253
265
}
254
266
255
267
ast:: ItemKind :: Impl { unsafety : ast:: Unsafety :: Unsafe , .. } => {
256
- self . report_unsafe ( cx, it. span , |lint| lint. build ( "implementation of an `unsafe` trait" ) . emit ( ) )
268
+ self . report_unsafe ( cx, it. span , |lint| {
269
+ lint. build ( "implementation of an `unsafe` trait" ) . emit ( )
270
+ } )
257
271
}
258
272
259
273
_ => return ,
@@ -269,13 +283,16 @@ impl EarlyLintPass for UnsafeCode {
269
283
_: ast:: NodeId ,
270
284
) {
271
285
match fk {
272
- FnKind :: ItemFn ( _, ast:: FnHeader { unsafety : ast:: Unsafety :: Unsafe , .. } , ..) => {
273
- self . report_unsafe ( cx, span, |lint| lint. build ( "declaration of an `unsafe` function" ) . emit ( ) )
274
- }
286
+ FnKind :: ItemFn ( _, ast:: FnHeader { unsafety : ast:: Unsafety :: Unsafe , .. } , ..) => self
287
+ . report_unsafe ( cx, span, |lint| {
288
+ lint. build ( "declaration of an `unsafe` function" ) . emit ( )
289
+ } ) ,
275
290
276
291
FnKind :: Method ( _, sig, ..) => {
277
292
if sig. header . unsafety == ast:: Unsafety :: Unsafe {
278
- self . report_unsafe ( cx, span, |lint| lint. build ( "implementation of an `unsafe` method" ) . emit ( ) )
293
+ self . report_unsafe ( cx, span, |lint| {
294
+ lint. build ( "implementation of an `unsafe` method" ) . emit ( )
295
+ } )
279
296
}
280
297
}
281
298
@@ -286,7 +303,9 @@ impl EarlyLintPass for UnsafeCode {
286
303
fn check_trait_item ( & mut self , cx : & EarlyContext < ' _ > , item : & ast:: AssocItem ) {
287
304
if let ast:: AssocItemKind :: Fn ( ref sig, None ) = item. kind {
288
305
if sig. header . unsafety == ast:: Unsafety :: Unsafe {
289
- self . report_unsafe ( cx, item. span , |lint| lint. build ( "declaration of an `unsafe` method" ) . emit ( ) )
306
+ self . report_unsafe ( cx, item. span , |lint| {
307
+ lint. build ( "declaration of an `unsafe` method" ) . emit ( )
308
+ } )
290
309
}
291
310
}
292
311
}
@@ -372,11 +391,9 @@ impl MissingDoc {
372
391
373
392
let has_doc = attrs. iter ( ) . any ( |a| has_doc ( a) ) ;
374
393
if !has_doc {
375
- cx. struct_span_lint (
376
- MISSING_DOCS ,
377
- cx. tcx . sess . source_map ( ) . def_span ( sp) ,
378
- |lint| lint. build ( & format ! ( "missing documentation for {}" , desc) ) . emit ( ) ,
379
- ) ;
394
+ cx. struct_span_lint ( MISSING_DOCS , cx. tcx . sess . source_map ( ) . def_span ( sp) , |lint| {
395
+ lint. build ( & format ! ( "missing documentation for {}" , desc) ) . emit ( )
396
+ } ) ;
380
397
}
381
398
}
382
399
}
@@ -555,12 +572,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingCopyImplementations {
555
572
return ;
556
573
}
557
574
if can_type_implement_copy ( cx. tcx , param_env, ty) . is_ok ( ) {
558
- cx. struct_span_lint (
559
- MISSING_COPY_IMPLEMENTATIONS ,
560
- item. span ,
561
- |lint| lint. build ( "type could implement `Copy`; consider adding `impl \
562
- Copy`") . emit ( ) ,
563
- )
575
+ cx. struct_span_lint ( MISSING_COPY_IMPLEMENTATIONS , item. span , |lint| {
576
+ lint. build (
577
+ "type could implement `Copy`; consider adding `impl \
578
+ Copy`",
579
+ )
580
+ . emit ( )
581
+ } )
564
582
}
565
583
}
566
584
}
@@ -609,12 +627,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
609
627
}
610
628
611
629
if !self . impling_types . as_ref ( ) . unwrap ( ) . contains ( & item. hir_id ) {
612
- cx. struct_span_lint (
613
- MISSING_DEBUG_IMPLEMENTATIONS ,
614
- item. span ,
615
- |lint| lint. build ( "type does not implement `fmt::Debug`; consider adding `#[derive(Debug)]` \
616
- or a manual implementation") . emit ( ) ,
617
- )
630
+ cx. struct_span_lint ( MISSING_DEBUG_IMPLEMENTATIONS , item. span , |lint| {
631
+ lint. build (
632
+ "type does not implement `fmt::Debug`; consider adding `#[derive(Debug)]` \
633
+ or a manual implementation",
634
+ )
635
+ . emit ( )
636
+ } )
618
637
}
619
638
}
620
639
}
@@ -912,7 +931,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes {
912
931
match get_transmute_from_to ( cx, expr) . map ( |( ty1, ty2) | ( & ty1. kind , & ty2. kind ) ) {
913
932
Some ( ( & ty:: Ref ( _, _, from_mt) , & ty:: Ref ( _, _, to_mt) ) ) => {
914
933
if to_mt == hir:: Mutability :: Mut && from_mt == hir:: Mutability :: Not {
915
- cx. struct_span_lint ( MUTABLE_TRANSMUTES , expr. span , |lint| lint. build ( msg) . emit ( ) ) ;
934
+ cx. struct_span_lint ( MUTABLE_TRANSMUTES , expr. span , |lint| {
935
+ lint. build ( msg) . emit ( )
936
+ } ) ;
916
937
}
917
938
}
918
939
_ => ( ) ,
@@ -962,7 +983,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnstableFeatures {
962
983
if attr. check_name ( sym:: feature) {
963
984
if let Some ( items) = attr. meta_item_list ( ) {
964
985
for item in items {
965
- ctx. struct_span_lint ( UNSTABLE_FEATURES , item. span ( ) , |lint| lint. build ( "unstable feature" ) . emit ( ) ) ;
986
+ ctx. struct_span_lint ( UNSTABLE_FEATURES , item. span ( ) , |lint| {
987
+ lint. build ( "unstable feature" ) . emit ( )
988
+ } ) ;
966
989
}
967
990
}
968
991
}
@@ -1244,15 +1267,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
1244
1267
ConstEvaluatable ( ..) => continue ,
1245
1268
} ;
1246
1269
if predicate. is_global ( ) {
1247
- cx. struct_span_lint (
1248
- TRIVIAL_BOUNDS ,
1249
- span,
1250
- |lint| lint. build ( & format ! (
1270
+ cx. struct_span_lint ( TRIVIAL_BOUNDS , span, |lint| {
1271
+ lint. build ( & format ! (
1251
1272
"{} bound {} does not depend on any type \
1252
1273
or lifetime parameters",
1253
1274
predicate_kind_name, predicate
1254
- ) ) . emit ( ) ,
1255
- ) ;
1275
+ ) )
1276
+ . emit ( )
1277
+ } ) ;
1256
1278
}
1257
1279
}
1258
1280
}
0 commit comments