parent
652b4c720d
commit
3ab6aeefb1
@ -4641,6 +4641,7 @@ Released 2018-09-13
|
||||
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
|
||||
[`await_holding_refcell_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
|
||||
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
|
||||
[`big_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#big_endian_bytes
|
||||
[`bind_instead_of_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
|
||||
[`blacklisted_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name
|
||||
[`blanket_clippy_restriction_lints`]: https://rust-lang.github.io/rust-clippy/master/index.html#blanket_clippy_restriction_lints
|
||||
@ -4811,6 +4812,7 @@ Released 2018-09-13
|
||||
[`get_first`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_first
|
||||
[`get_last_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_last_with_len
|
||||
[`get_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_unwrap
|
||||
[`host_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#host_endian_bytes
|
||||
[`identity_conversion`]: https://rust-lang.github.io/rust-clippy/master/index.html#identity_conversion
|
||||
[`identity_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
|
||||
[`if_let_mutex`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_let_mutex
|
||||
@ -4892,6 +4894,7 @@ Released 2018-09-13
|
||||
[`let_with_type_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_with_type_underscore
|
||||
[`lines_filter_map_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#lines_filter_map_ok
|
||||
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
|
||||
[`little_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#little_endian_bytes
|
||||
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
|
||||
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
|
||||
[`macro_use_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#macro_use_imports
|
||||
|
@ -143,6 +143,9 @@
|
||||
crate::empty_drop::EMPTY_DROP_INFO,
|
||||
crate::empty_enum::EMPTY_ENUM_INFO,
|
||||
crate::empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS_INFO,
|
||||
crate::endian_bytes::BIG_ENDIAN_BYTES_INFO,
|
||||
crate::endian_bytes::HOST_ENDIAN_BYTES_INFO,
|
||||
crate::endian_bytes::LITTLE_ENDIAN_BYTES_INFO,
|
||||
crate::entry::MAP_ENTRY_INFO,
|
||||
crate::enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT_INFO,
|
||||
crate::enum_variants::ENUM_VARIANT_NAMES_INFO,
|
||||
|
138
clippy_lints/src/endian_bytes.rs
Normal file
138
clippy_lints/src/endian_bytes.rs
Normal file
@ -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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -114,6 +114,7 @@
|
||||
mod empty_drop;
|
||||
mod empty_enum;
|
||||
mod empty_structs_with_brackets;
|
||||
mod endian_bytes;
|
||||
mod entry;
|
||||
mod enum_clike;
|
||||
mod enum_variants;
|
||||
@ -996,6 +997,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs));
|
||||
store.register_early_pass(|| Box::new(needless_else::NeedlessElse));
|
||||
store.register_late_pass(|_| Box::new(missing_fields_in_debug::MissingFieldsInDebug));
|
||||
store.register_late_pass(|_| Box::new(endian_bytes::EndianBytes));
|
||||
// add lints here, do not remove this comment, it's used in `new_lint`
|
||||
}
|
||||
|
||||
|
163
tests/ui/endian_bytes.rs
Normal file
163
tests/ui/endian_bytes.rs
Normal file
@ -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!());
|
||||
}
|
1152
tests/ui/endian_bytes.stderr
Normal file
1152
tests/ui/endian_bytes.stderr
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user