@@ -2,6 +2,7 @@ mod cast_lossless;
2
2
mod cast_possible_truncation;
3
3
mod cast_possible_wrap;
4
4
mod cast_precision_loss;
5
+ mod cast_ptr_alignment;
5
6
mod cast_sign_loss;
6
7
mod fn_to_numeric_cast;
7
8
mod fn_to_numeric_cast_with_truncation;
@@ -13,22 +14,18 @@ use std::borrow::Cow;
13
14
use if_chain:: if_chain;
14
15
use rustc_ast:: LitKind ;
15
16
use rustc_errors:: Applicability ;
16
- use rustc_hir:: { Expr , ExprKind , GenericArg , MutTy , Mutability , TyKind , UnOp } ;
17
+ use rustc_hir:: { Expr , ExprKind , MutTy , Mutability , TyKind , UnOp } ;
17
18
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
18
19
use rustc_middle:: lint:: in_external_macro;
19
- use rustc_middle:: ty:: { self , Ty , TypeAndMut , UintTy } ;
20
+ use rustc_middle:: ty:: { self , TypeAndMut , UintTy } ;
20
21
use rustc_semver:: RustcVersion ;
21
22
use rustc_session:: { declare_lint_pass, declare_tool_lint, impl_lint_pass} ;
22
- use rustc_span:: symbol:: sym;
23
- use rustc_target:: abi:: LayoutOf ;
24
23
25
24
use crate :: utils:: sugg:: Sugg ;
26
25
use crate :: utils:: {
27
26
is_hir_ty_cfg_dependant, meets_msrv, snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then,
28
27
} ;
29
28
30
- use utils:: int_ty_to_nbits;
31
-
32
29
declare_clippy_lint ! {
33
30
/// **What it does:** Checks for casts from any numerical to a float type where
34
31
/// the receiving type cannot store all values from the original type without
@@ -270,22 +267,6 @@ declare_lint_pass!(Casts => [
270
267
FN_TO_NUMERIC_CAST_WITH_TRUNCATION ,
271
268
] ) ;
272
269
273
- /// Check if the given type is either `core::ffi::c_void` or
274
- /// one of the platform specific `libc::<platform>::c_void` of libc.
275
- fn is_c_void ( cx : & LateContext < ' _ > , ty : Ty < ' _ > ) -> bool {
276
- if let ty:: Adt ( adt, _) = ty. kind ( ) {
277
- let names = cx. get_def_path ( adt. did ) ;
278
-
279
- if names. is_empty ( ) {
280
- return false ;
281
- }
282
- if names[ 0 ] == sym:: libc || names[ 0 ] == sym:: core && * names. last ( ) . unwrap ( ) == sym ! ( c_void) {
283
- return true ;
284
- }
285
- }
286
- false
287
- }
288
-
289
270
impl < ' tcx > LateLintPass < ' tcx > for Casts {
290
271
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
291
272
if expr. span . from_expansion ( ) {
@@ -306,56 +287,16 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
306
287
307
288
fn_to_numeric_cast:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
308
289
fn_to_numeric_cast_with_truncation:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
309
- lint_cast_ptr_alignment ( cx, expr, cast_from, cast_to) ;
310
290
if cast_from. is_numeric ( ) && cast_to. is_numeric ( ) && !in_external_macro ( cx. sess ( ) , expr. span ) {
311
291
cast_possible_truncation:: check ( cx, expr, cast_from, cast_to) ;
312
292
cast_possible_wrap:: check ( cx, expr, cast_from, cast_to) ;
313
293
cast_precision_loss:: check ( cx, expr, cast_from, cast_to) ;
314
294
cast_lossless:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
315
295
cast_sign_loss:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
316
296
}
317
- } else if let ExprKind :: MethodCall ( method_path, _, args, _) = expr. kind {
318
- if_chain ! {
319
- if method_path. ident. name == sym!( cast) ;
320
- if let Some ( generic_args) = method_path. args;
321
- if let [ GenericArg :: Type ( cast_to) ] = generic_args. args;
322
- // There probably is no obvious reason to do this, just to be consistent with `as` cases.
323
- if !is_hir_ty_cfg_dependant( cx, cast_to) ;
324
- then {
325
- let ( cast_from, cast_to) =
326
- ( cx. typeck_results( ) . expr_ty( & args[ 0 ] ) , cx. typeck_results( ) . expr_ty( expr) ) ;
327
- lint_cast_ptr_alignment( cx, expr, cast_from, cast_to) ;
328
- }
329
- }
330
297
}
331
- }
332
- }
333
298
334
- fn lint_cast_ptr_alignment < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' _ > , cast_from : Ty < ' tcx > , cast_to : Ty < ' tcx > ) {
335
- if_chain ! {
336
- if let ty:: RawPtr ( from_ptr_ty) = & cast_from. kind( ) ;
337
- if let ty:: RawPtr ( to_ptr_ty) = & cast_to. kind( ) ;
338
- if let Ok ( from_layout) = cx. layout_of( from_ptr_ty. ty) ;
339
- if let Ok ( to_layout) = cx. layout_of( to_ptr_ty. ty) ;
340
- if from_layout. align. abi < to_layout. align. abi;
341
- // with c_void, we inherently need to trust the user
342
- if !is_c_void( cx, from_ptr_ty. ty) ;
343
- // when casting from a ZST, we don't know enough to properly lint
344
- if !from_layout. is_zst( ) ;
345
- then {
346
- span_lint(
347
- cx,
348
- CAST_PTR_ALIGNMENT ,
349
- expr. span,
350
- & format!(
351
- "casting from `{}` to a more-strictly-aligned pointer (`{}`) ({} < {} bytes)" ,
352
- cast_from,
353
- cast_to,
354
- from_layout. align. abi. bytes( ) ,
355
- to_layout. align. abi. bytes( ) ,
356
- ) ,
357
- ) ;
358
- }
299
+ cast_ptr_alignment:: check ( cx, expr) ;
359
300
}
360
301
}
361
302
0 commit comments