implement host_endian_bytes and the other two

This commit is contained in:
Centri3 2023-05-25 11:02:54 -05:00
parent 3ab6aeefb1
commit 97a0ccc1d3
3 changed files with 1142 additions and 1240 deletions

View file

@ -1,29 +1,11 @@
use clippy_utils::{
diagnostics::{span_lint_and_help, span_lint_and_then},
is_lint_allowed, match_def_path, path_def_id,
};
use crate::Lint;
use clippy_utils::{diagnostics::span_lint_and_then, is_lint_allowed};
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
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"
}
use rustc_span::Symbol;
use std::borrow::Cow;
declare_clippy_lint! {
/// ### What it does
@ -38,9 +20,9 @@ declare_clippy_lint! {
/// let _y = 2i64.to_ne_bytes();
/// ```
#[clippy::version = "1.71.0"]
pub LITTLE_ENDIAN_BYTES,
pub HOST_ENDIAN_BYTES,
restriction,
"disallows usage of the `to_le_bytes` method"
"disallows usage of the `to_ne_bytes` method"
}
declare_clippy_lint! {
@ -48,10 +30,32 @@ declare_clippy_lint! {
/// Checks for the usage of the `to_le_bytes` method.
///
/// ### Why is this bad?
/// It's not, but some may wish to lint usages of this method, either to suggest using the host
/// endianness or big endian.
///
/// ### Example
/// ```rust,ignore
/// // example code where clippy issues a warning
/// let _x = 2i32.to_le_bytes();
/// let _y = 2i64.to_le_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_be_bytes` method.
///
/// ### Why is this bad?
/// It's not, but some may wish to lint usages of this method, either to suggest using the host
/// endianness or little endian.
///
/// ### Example
/// ```rust,ignore
/// let _x = 2i32.to_be_bytes();
/// let _y = 2i64.to_be_bytes();
/// ```
#[clippy::version = "1.71.0"]
pub BIG_ENDIAN_BYTES,
@ -61,78 +65,133 @@ declare_clippy_lint! {
declare_lint_pass!(EndianBytes => [HOST_ENDIAN_BYTES, LITTLE_ENDIAN_BYTES, BIG_ENDIAN_BYTES]);
#[derive(Clone, Debug)]
enum LintKind {
Host,
Little,
Big,
}
impl LintKind {
fn allowed(&self, cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
is_lint_allowed(cx, self.as_lint(), expr.hir_id)
}
fn as_lint(&self) -> &'static Lint {
match self {
LintKind::Host => HOST_ENDIAN_BYTES,
LintKind::Little => LITTLE_ENDIAN_BYTES,
LintKind::Big => BIG_ENDIAN_BYTES,
}
}
fn to_name(&self, prefix: &str) -> String {
match self {
LintKind::Host => format!("{prefix}_ne_bytes"),
LintKind::Little => format!("{prefix}_le_bytes"),
LintKind::Big => format!("{prefix}_be_bytes"),
}
}
}
impl LateLintPass<'_> for EndianBytes {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if in_external_macro(cx.sess(), expr.span) {
return;
}
if_chain! {
if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind;
if let ExprKind::Lit(..) = receiver.kind;
if args.is_empty();
if try_lint_endian_bytes(cx, expr, "to", method_name.ident.name);
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;
if let ExprKind::Call(function, ..) = expr.kind;
if let ExprKind::Path(qpath) = function.kind;
if let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id();
if let Some(function_name) = cx.get_def_path(def_id).last();
if cx.typeck_results().expr_ty(expr).is_primitive_ty();
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");
}
});
}
try_lint_endian_bytes(cx, expr, "from", *function_name);
}
}
}
}
fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol) -> bool {
let ne = format!("{prefix}_ne_bytes");
let le = format!("{prefix}_le_bytes");
let be = format!("{prefix}_be_bytes");
let (lint, other_lints) = match name.as_str() {
name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),
name if name == le => ((&LintKind::Little), [(&LintKind::Host), (&LintKind::Big)]),
name if name == be => ((&LintKind::Big), [(&LintKind::Host), (&LintKind::Little)]),
_ => return false,
};
let mut help = None;
'build_help: {
// all lints disallowed, don't give help here
if [&[lint], other_lints.as_slice()]
.concat()
.iter()
.all(|lint| !lint.allowed(cx, expr))
{
break 'build_help;
}
// ne_bytes and all other lints allowed
if lint.to_name(prefix) == ne && other_lints.iter().all(|lint| lint.allowed(cx, expr)) {
help = Some(Cow::Borrowed("specify the desired endianness explicitly"));
break 'build_help;
}
// le_bytes where ne_bytes allowed but be_bytes is not, or le_bytes where ne_bytes allowed but
// le_bytes is not
if (lint.to_name(prefix) == le || lint.to_name(prefix) == be) && LintKind::Host.allowed(cx, expr) {
help = Some(Cow::Borrowed("use the native endianness instead"));
break 'build_help;
}
let allowed_lints = other_lints.iter().filter(|lint| lint.allowed(cx, expr));
let len = allowed_lints.clone().count();
let mut help_str = "use ".to_owned();
for (i, lint) in allowed_lints.enumerate() {
let only_one = len == 1;
if !only_one {
help_str.push_str("either of ");
}
help_str.push_str(&format!("`{}` ", lint.to_name(prefix)));
if i != len && !only_one {
help_str.push_str("or ");
}
}
help = Some(Cow::Owned(help_str + "instead"));
}
span_lint_and_then(
cx,
lint.as_lint(),
expr.span,
&format!("usage of the method `{}`", lint.to_name(prefix)),
move |diag| {
if let Some(help) = help {
diag.help(help);
}
},
);
true
}

View file

@ -2,162 +2,126 @@
#![allow(clippy::diverging_sub_expression)]
#![no_main]
macro_rules! fn_body {
() => {
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();
2.0f32.to_ne_bytes();
2.0f64.to_ne_bytes();
2usize.to_ne_bytes();
2isize.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!());
usize::from_ne_bytes(todo!());
isize::from_ne_bytes(todo!());
f32::from_ne_bytes(todo!());
f64::from_ne_bytes(todo!());
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();
2.0f32.to_le_bytes();
2.0f64.to_le_bytes();
2usize.to_le_bytes();
2isize.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!());
};
}
// bless breaks if I use fn_body too much (oops)
macro_rules! fn_body_small {
() => {
2u8.to_ne_bytes();
u8::from_ne_bytes(todo!());
2u8.to_le_bytes();
u8::from_le_bytes(todo!());
2u8.to_be_bytes();
u8::from_be_bytes(todo!());
};
}
#[rustfmt::skip]
#[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!());
}
fn host() { fn_body!(); }
#[rustfmt::skip]
#[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!());
}
fn little() { fn_body!(); }
#[rustfmt::skip]
#[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!());
}
fn big() { fn_body!(); }
#[rustfmt::skip]
#[warn(clippy::host_endian_bytes)]
#[warn(clippy::big_endian_bytes)]
fn host_encourage_little() { fn_body_small!(); }
#[rustfmt::skip]
#[warn(clippy::host_endian_bytes)]
#[warn(clippy::little_endian_bytes)]
fn host_encourage_big() { fn_body_small!(); }
#[rustfmt::skip]
#[warn(clippy::host_endian_bytes)]
#[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!());
}
fn no_help() { fn_body_small!(); }
#[rustfmt::skip]
#[warn(clippy::little_endian_bytes)]
#[warn(clippy::big_endian_bytes)]
fn little_encourage_host() { fn_body_small!(); }
#[rustfmt::skip]
#[warn(clippy::host_endian_bytes)]
#[warn(clippy::little_endian_bytes)]
fn little_encourage_big() { fn_body_small!(); }
#[rustfmt::skip]
#[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!());
}
fn big_encourage_host() { fn_body_small!(); }
#[rustfmt::skip]
#[warn(clippy::host_endian_bytes)]
#[warn(clippy::big_endian_bytes)]
fn big_encourage_little() { fn_body_small!(); }

File diff suppressed because one or more lines are too long