2018-10-06 16:18:06 +00:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#![feature(tool_lints)]
|
|
|
|
|
2018-04-11 09:17:59 +00:00
|
|
|
//! Test casts for alignment issues
|
|
|
|
|
2018-04-18 00:13:19 +00:00
|
|
|
#![feature(libc)]
|
|
|
|
|
|
|
|
extern crate libc;
|
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#[warn(clippy::cast_ptr_alignment)]
|
|
|
|
#[allow(clippy::no_effect, clippy::unnecessary_operation, clippy::cast_lossless)]
|
2018-04-11 09:17:59 +00:00
|
|
|
fn main() {
|
|
|
|
/* These should be warned against */
|
|
|
|
|
|
|
|
// cast to more-strictly-aligned type
|
|
|
|
(&1u8 as *const u8) as *const u16;
|
|
|
|
(&mut 1u8 as *mut u8) as *mut u16;
|
|
|
|
|
|
|
|
/* These should be okay */
|
|
|
|
|
|
|
|
// not a pointer type
|
|
|
|
1u8 as u16;
|
|
|
|
// cast to less-strictly-aligned type
|
|
|
|
(&1u16 as *const u16) as *const u8;
|
|
|
|
(&mut 1u16 as *mut u16) as *mut u8;
|
2018-04-18 00:13:19 +00:00
|
|
|
// For c_void, we should trust the user. See #2677
|
|
|
|
(&1u32 as *const u32 as *const std::os::raw::c_void) as *const u32;
|
|
|
|
(&1u32 as *const u32 as *const libc::c_void) as *const u32;
|
2018-04-11 09:17:59 +00:00
|
|
|
}
|