Skip to content

Commit 8438583

Browse files
authored
Merge pull request #2679 from devonhollowood/cast-alignment-cvoid
Make cast_ptr_alignment ignore c_void
2 parents 9a37566 + f0e09d4 commit 8438583

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

clippy_lints/src/types.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use syntax::ast::{FloatTy, IntTy, UintTy};
1313
use syntax::codemap::Span;
1414
use syntax::errors::DiagnosticBuilder;
1515
use utils::{comparisons, higher, in_constant, in_external_macro, in_macro, last_path_segment, match_def_path, match_path,
16-
multispan_sugg, opt_def_id, same_tys, snippet, snippet_opt, span_help_and_lint, span_lint,
16+
match_type, multispan_sugg, opt_def_id, same_tys, snippet, snippet_opt, span_help_and_lint, span_lint,
1717
span_lint_and_sugg, span_lint_and_then, clip, unsext, sext, int_bits};
1818
use utils::paths;
1919
use consts::{constant, Constant};
@@ -981,6 +981,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
981981
if let Some(from_align) = cx.layout_of(from_ptr_ty.ty).ok().map(|a| a.align.abi());
982982
if let Some(to_align) = cx.layout_of(to_ptr_ty.ty).ok().map(|a| a.align.abi());
983983
if from_align < to_align;
984+
// with c_void, we inherently need to trust the user
985+
if ! (
986+
match_type(cx, from_ptr_ty.ty, &paths::C_VOID)
987+
|| match_type(cx, from_ptr_ty.ty, &paths::C_VOID_LIBC)
988+
);
984989
then {
985990
span_lint(
986991
cx,

clippy_lints/src/utils/paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub const CMP_MAX: [&str; 3] = ["core", "cmp", "max"];
2020
pub const CMP_MIN: [&str; 3] = ["core", "cmp", "min"];
2121
pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"];
2222
pub const CSTRING_NEW: [&str; 5] = ["std", "ffi", "c_str", "CString", "new"];
23+
pub const C_VOID: [&str; 4] = ["std", "os", "raw", "c_void"];
24+
pub const C_VOID_LIBC: [&str; 2] = ["libc", "c_void"];
2325
pub const DEBUG_FMT_METHOD: [&str; 4] = ["core", "fmt", "Debug", "fmt"];
2426
pub const DEFAULT_TRAIT: [&str; 3] = ["core", "default", "Default"];
2527
pub const DISPLAY_FMT_METHOD: [&str; 4] = ["core", "fmt", "Display", "fmt"];

tests/ui/cast_alignment.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Test casts for alignment issues
22
3+
#![feature(libc)]
4+
5+
extern crate libc;
6+
37
#[warn(cast_ptr_alignment)]
48
#[allow(no_effect, unnecessary_operation, cast_lossless)]
59
fn main() {
@@ -16,4 +20,7 @@ fn main() {
1620
// cast to less-strictly-aligned type
1721
(&1u16 as *const u16) as *const u8;
1822
(&mut 1u16 as *mut u16) as *mut u8;
23+
// For c_void, we should trust the user. See #2677
24+
(&1u32 as *const u32 as *const std::os::raw::c_void) as *const u32;
25+
(&1u32 as *const u32 as *const libc::c_void) as *const u32;
1926
}

tests/ui/cast_alignment.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`)
2-
--> $DIR/cast_alignment.rs:9:5
3-
|
4-
9 | (&1u8 as *const u8) as *const u16;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: `-D cast-ptr-alignment` implied by `-D warnings`
2+
--> $DIR/cast_alignment.rs:13:5
3+
|
4+
13 | (&1u8 as *const u8) as *const u16;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D cast-ptr-alignment` implied by `-D warnings`
88

99
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`)
10-
--> $DIR/cast_alignment.rs:10:5
10+
--> $DIR/cast_alignment.rs:14:5
1111
|
12-
10 | (&mut 1u8 as *mut u8) as *mut u16;
12+
14 | (&mut 1u8 as *mut u8) as *mut u16;
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)