forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to_xx_bytes
implemented, from_xx_bytes
todo
Mentioned in rust-lang#10765
- Loading branch information
Showing
6 changed files
with
1,461 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use clippy_utils::{ | ||
diagnostics::{span_lint_and_help, span_lint_and_then}, | ||
is_lint_allowed, match_def_path, path_def_id, | ||
}; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// // example code where clippy issues a warning | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// // example code which does not raise clippy warning | ||
/// ``` | ||
#[clippy::version = "1.71.0"] | ||
pub HOST_ENDIAN_BYTES, | ||
restriction, | ||
"disallows usage of the `to_ne_bytes` method" | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for the usage of the `to_ne_bytes` method. | ||
/// | ||
/// ### Why is this bad? | ||
/// It's not, but some may prefer to specify the target endianness explicitly. | ||
/// | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// let _x = 2i32.to_ne_bytes(); | ||
/// let _y = 2i64.to_ne_bytes(); | ||
/// ``` | ||
#[clippy::version = "1.71.0"] | ||
pub LITTLE_ENDIAN_BYTES, | ||
restriction, | ||
"disallows usage of the `to_le_bytes` method" | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for the usage of the `to_le_bytes` method. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// // example code where clippy issues a warning | ||
/// ``` | ||
#[clippy::version = "1.71.0"] | ||
pub BIG_ENDIAN_BYTES, | ||
restriction, | ||
"disallows usage of the `to_be_bytes` method" | ||
} | ||
|
||
declare_lint_pass!(EndianBytes => [HOST_ENDIAN_BYTES, LITTLE_ENDIAN_BYTES, BIG_ENDIAN_BYTES]); | ||
|
||
impl LateLintPass<'_> for EndianBytes { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind; | ||
if let ExprKind::Lit(..) = receiver.kind; | ||
if args.is_empty(); | ||
then { | ||
if method_name.ident.name == sym!(to_ne_bytes) { | ||
span_lint_and_help( | ||
cx, | ||
HOST_ENDIAN_BYTES, | ||
expr.span, | ||
"use of the method `to_ne_bytes`", | ||
None, | ||
"consider specifying the desired endianness", | ||
); | ||
} else if method_name.ident.name == sym!(to_le_bytes) { | ||
span_lint_and_then(cx, LITTLE_ENDIAN_BYTES, expr.span, "use of the method `to_le_bytes`", |diag| { | ||
if is_lint_allowed(cx, BIG_ENDIAN_BYTES, expr.hir_id) { | ||
diag.help("use `to_be_bytes` instead"); | ||
} | ||
}); | ||
} else if method_name.ident.name == sym!(to_be_bytes) { | ||
span_lint_and_then(cx, BIG_ENDIAN_BYTES, expr.span, "use of the method `to_be_bytes`", |diag| { | ||
if is_lint_allowed(cx, LITTLE_ENDIAN_BYTES, expr.hir_id) { | ||
diag.help("use `to_le_bytes` instead"); | ||
} | ||
}); | ||
} | ||
|
||
// don't waste time also checking from_**_bytes | ||
return; | ||
} | ||
} | ||
|
||
span_lint_and_help( | ||
cx, | ||
HOST_ENDIAN_BYTES, | ||
expr.span, | ||
"use of the method `from_ne_bytes`", | ||
None, | ||
&format!("consider specifying the desired endianness: {expr:?}"), | ||
); | ||
|
||
if_chain! { | ||
if let ExprKind::Call(function, args) = expr.kind; | ||
if let Some(function_def_id) = path_def_id(cx, function); | ||
if args.len() == 1; | ||
then { | ||
if match_def_path(cx, function_def_id, &["from_ne_bytes"]) { | ||
span_lint_and_help( | ||
cx, | ||
HOST_ENDIAN_BYTES, | ||
expr.span, | ||
"use of the method `from_ne_bytes`", | ||
None, | ||
"consider specifying the desired endianness", | ||
); | ||
} else if match_def_path(cx, function_def_id, &["from_le_bytes"]) { | ||
span_lint_and_then(cx, LITTLE_ENDIAN_BYTES, expr.span, "use of the method `from_le_bytes`", |diag| { | ||
if is_lint_allowed(cx, BIG_ENDIAN_BYTES, expr.hir_id) { | ||
diag.help("use `from_be_bytes` instead"); | ||
} | ||
}); | ||
} else if match_def_path(cx, function_def_id, &["from_be_bytes"]) { | ||
span_lint_and_then(cx, BIG_ENDIAN_BYTES, expr.span, "use of the method `from_be_bytes`", |diag| { | ||
if is_lint_allowed(cx, LITTLE_ENDIAN_BYTES, expr.hir_id) { | ||
diag.help("use `from_le_bytes` instead"); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#![allow(unused)] | ||
#![allow(clippy::diverging_sub_expression)] | ||
#![no_main] | ||
|
||
#[warn(clippy::host_endian_bytes)] | ||
fn host() { | ||
2u8.to_ne_bytes(); | ||
2i8.to_ne_bytes(); | ||
2u16.to_ne_bytes(); | ||
2i16.to_ne_bytes(); | ||
2u32.to_ne_bytes(); | ||
2i32.to_ne_bytes(); | ||
2u64.to_ne_bytes(); | ||
2i64.to_ne_bytes(); | ||
2u128.to_ne_bytes(); | ||
2i128.to_ne_bytes(); | ||
2usize.to_ne_bytes(); | ||
2isize.to_ne_bytes(); | ||
2.0f32.to_ne_bytes(); | ||
2.0f64.to_ne_bytes(); | ||
u8::from_ne_bytes(todo!()); | ||
i8::from_ne_bytes(todo!()); | ||
u16::from_ne_bytes(todo!()); | ||
i16::from_ne_bytes(todo!()); | ||
u32::from_ne_bytes(todo!()); | ||
i32::from_ne_bytes(todo!()); | ||
u64::from_ne_bytes(todo!()); | ||
i64::from_ne_bytes(todo!()); | ||
u128::from_ne_bytes(todo!()); | ||
i128::from_ne_bytes(todo!()); | ||
f32::from_ne_bytes(todo!()); | ||
f64::from_ne_bytes(todo!()); | ||
} | ||
|
||
#[warn(clippy::little_endian_bytes)] | ||
fn little() { | ||
2u8.to_le_bytes(); | ||
2i8.to_le_bytes(); | ||
2u16.to_le_bytes(); | ||
2i16.to_le_bytes(); | ||
2u32.to_le_bytes(); | ||
2i32.to_le_bytes(); | ||
2u64.to_le_bytes(); | ||
2i64.to_le_bytes(); | ||
2u128.to_le_bytes(); | ||
2i128.to_le_bytes(); | ||
2usize.to_le_bytes(); | ||
2isize.to_le_bytes(); | ||
2.0f32.to_le_bytes(); | ||
2.0f64.to_le_bytes(); | ||
u8::from_le_bytes(todo!()); | ||
i8::from_le_bytes(todo!()); | ||
u16::from_le_bytes(todo!()); | ||
i16::from_le_bytes(todo!()); | ||
u32::from_le_bytes(todo!()); | ||
i32::from_le_bytes(todo!()); | ||
u64::from_le_bytes(todo!()); | ||
i64::from_le_bytes(todo!()); | ||
u128::from_le_bytes(todo!()); | ||
i128::from_le_bytes(todo!()); | ||
usize::from_le_bytes(todo!()); | ||
isize::from_le_bytes(todo!()); | ||
f32::from_le_bytes(todo!()); | ||
f64::from_le_bytes(todo!()); | ||
} | ||
|
||
#[warn(clippy::big_endian_bytes)] | ||
fn big() { | ||
2u8.to_be_bytes(); | ||
2i8.to_be_bytes(); | ||
2u16.to_be_bytes(); | ||
2i16.to_be_bytes(); | ||
2u32.to_be_bytes(); | ||
2i32.to_be_bytes(); | ||
2u64.to_be_bytes(); | ||
2i64.to_be_bytes(); | ||
2u128.to_be_bytes(); | ||
2i128.to_be_bytes(); | ||
2.0f32.to_be_bytes(); | ||
2.0f64.to_be_bytes(); | ||
2usize.to_be_bytes(); | ||
2isize.to_be_bytes(); | ||
u8::from_be_bytes(todo!()); | ||
i8::from_be_bytes(todo!()); | ||
u16::from_be_bytes(todo!()); | ||
i16::from_be_bytes(todo!()); | ||
u32::from_be_bytes(todo!()); | ||
i32::from_be_bytes(todo!()); | ||
u64::from_be_bytes(todo!()); | ||
i64::from_be_bytes(todo!()); | ||
u128::from_be_bytes(todo!()); | ||
i128::from_be_bytes(todo!()); | ||
usize::from_be_bytes(todo!()); | ||
isize::from_be_bytes(todo!()); | ||
f32::from_be_bytes(todo!()); | ||
f64::from_be_bytes(todo!()); | ||
} | ||
|
||
#[warn(clippy::little_endian_bytes)] | ||
#[warn(clippy::big_endian_bytes)] | ||
fn little_no_help() { | ||
2u8.to_le_bytes(); | ||
2i8.to_le_bytes(); | ||
2u16.to_le_bytes(); | ||
2i16.to_le_bytes(); | ||
2u32.to_le_bytes(); | ||
2i32.to_le_bytes(); | ||
2u64.to_le_bytes(); | ||
2i64.to_le_bytes(); | ||
2u128.to_le_bytes(); | ||
2i128.to_le_bytes(); | ||
2usize.to_le_bytes(); | ||
2isize.to_le_bytes(); | ||
2.0f32.to_le_bytes(); | ||
2.0f64.to_le_bytes(); | ||
u8::from_le_bytes(todo!()); | ||
i8::from_le_bytes(todo!()); | ||
u16::from_le_bytes(todo!()); | ||
i16::from_le_bytes(todo!()); | ||
u32::from_le_bytes(todo!()); | ||
i32::from_le_bytes(todo!()); | ||
u64::from_le_bytes(todo!()); | ||
i64::from_le_bytes(todo!()); | ||
u128::from_le_bytes(todo!()); | ||
i128::from_le_bytes(todo!()); | ||
usize::from_le_bytes(todo!()); | ||
isize::from_le_bytes(todo!()); | ||
f32::from_le_bytes(todo!()); | ||
f64::from_le_bytes(todo!()); | ||
} | ||
|
||
#[warn(clippy::big_endian_bytes)] | ||
#[warn(clippy::little_endian_bytes)] | ||
fn big_no_help() { | ||
2u8.to_be_bytes(); | ||
2i8.to_be_bytes(); | ||
2u16.to_be_bytes(); | ||
2i16.to_be_bytes(); | ||
2u32.to_be_bytes(); | ||
2i32.to_be_bytes(); | ||
2u64.to_be_bytes(); | ||
2i64.to_be_bytes(); | ||
2u128.to_be_bytes(); | ||
2i128.to_be_bytes(); | ||
2usize.to_be_bytes(); | ||
2isize.to_be_bytes(); | ||
2.0f32.to_be_bytes(); | ||
2.0f64.to_be_bytes(); | ||
u8::from_be_bytes(todo!()); | ||
i8::from_be_bytes(todo!()); | ||
u16::from_be_bytes(todo!()); | ||
i16::from_be_bytes(todo!()); | ||
u32::from_be_bytes(todo!()); | ||
i32::from_be_bytes(todo!()); | ||
u64::from_be_bytes(todo!()); | ||
i64::from_be_bytes(todo!()); | ||
u128::from_be_bytes(todo!()); | ||
i128::from_be_bytes(todo!()); | ||
usize::from_be_bytes(todo!()); | ||
isize::from_be_bytes(todo!()); | ||
f32::from_be_bytes(todo!()); | ||
f64::from_be_bytes(todo!()); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.