|
| 1 | +use crate::Lint; |
| 2 | +use clippy_utils::{diagnostics::span_lint_and_then, is_lint_allowed}; |
| 3 | +use rustc_hir::{Expr, ExprKind}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 5 | +use rustc_middle::{lint::in_external_macro, ty::Ty}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::Symbol; |
| 8 | +use std::borrow::Cow; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for the usage of the `to_ne_bytes` method and/or the function `from_ne_bytes`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// It's not, but some may prefer to specify the target endianness explicitly. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust,ignore |
| 19 | + /// let _x = 2i32.to_ne_bytes(); |
| 20 | + /// let _y = 2i64.to_ne_bytes(); |
| 21 | + /// ``` |
| 22 | + #[clippy::version = "1.71.0"] |
| 23 | + pub HOST_ENDIAN_BYTES, |
| 24 | + restriction, |
| 25 | + "disallows usage of the `to_ne_bytes` method" |
| 26 | +} |
| 27 | + |
| 28 | +declare_clippy_lint! { |
| 29 | + /// ### What it does |
| 30 | + /// Checks for the usage of the `to_le_bytes` method and/or the function `from_le_bytes`. |
| 31 | + /// |
| 32 | + /// ### Why is this bad? |
| 33 | + /// It's not, but some may wish to lint usage of this method, either to suggest using the host |
| 34 | + /// endianness or big endian. |
| 35 | + /// |
| 36 | + /// ### Example |
| 37 | + /// ```rust,ignore |
| 38 | + /// let _x = 2i32.to_le_bytes(); |
| 39 | + /// let _y = 2i64.to_le_bytes(); |
| 40 | + /// ``` |
| 41 | + #[clippy::version = "1.71.0"] |
| 42 | + pub LITTLE_ENDIAN_BYTES, |
| 43 | + restriction, |
| 44 | + "disallows usage of the `to_le_bytes` method" |
| 45 | +} |
| 46 | + |
| 47 | +declare_clippy_lint! { |
| 48 | + /// ### What it does |
| 49 | + /// Checks for the usage of the `to_be_bytes` method and/or the function `from_be_bytes`. |
| 50 | + /// |
| 51 | + /// ### Why is this bad? |
| 52 | + /// It's not, but some may wish to lint usage of this method, either to suggest using the host |
| 53 | + /// endianness or little endian. |
| 54 | + /// |
| 55 | + /// ### Example |
| 56 | + /// ```rust,ignore |
| 57 | + /// let _x = 2i32.to_be_bytes(); |
| 58 | + /// let _y = 2i64.to_be_bytes(); |
| 59 | + /// ``` |
| 60 | + #[clippy::version = "1.71.0"] |
| 61 | + pub BIG_ENDIAN_BYTES, |
| 62 | + restriction, |
| 63 | + "disallows usage of the `to_be_bytes` method" |
| 64 | +} |
| 65 | + |
| 66 | +declare_lint_pass!(EndianBytes => [HOST_ENDIAN_BYTES, LITTLE_ENDIAN_BYTES, BIG_ENDIAN_BYTES]); |
| 67 | + |
| 68 | +const HOST_NAMES: [&str; 2] = ["from_ne_bytes", "to_ne_bytes"]; |
| 69 | +const LITTLE_NAMES: [&str; 2] = ["from_le_bytes", "to_le_bytes"]; |
| 70 | +const BIG_NAMES: [&str; 2] = ["from_be_bytes", "to_be_bytes"]; |
| 71 | + |
| 72 | +#[derive(Clone, Debug)] |
| 73 | +enum LintKind { |
| 74 | + Host, |
| 75 | + Little, |
| 76 | + Big, |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Clone, Copy, PartialEq)] |
| 80 | +enum Prefix { |
| 81 | + From, |
| 82 | + To, |
| 83 | +} |
| 84 | + |
| 85 | +impl LintKind { |
| 86 | + fn allowed(&self, cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 87 | + is_lint_allowed(cx, self.as_lint(), expr.hir_id) |
| 88 | + } |
| 89 | + |
| 90 | + fn as_lint(&self) -> &'static Lint { |
| 91 | + match self { |
| 92 | + LintKind::Host => HOST_ENDIAN_BYTES, |
| 93 | + LintKind::Little => LITTLE_ENDIAN_BYTES, |
| 94 | + LintKind::Big => BIG_ENDIAN_BYTES, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn as_name(&self, prefix: Prefix) -> &str { |
| 99 | + let index = usize::from(prefix == Prefix::To); |
| 100 | + |
| 101 | + match self { |
| 102 | + LintKind::Host => HOST_NAMES[index], |
| 103 | + LintKind::Little => LITTLE_NAMES[index], |
| 104 | + LintKind::Big => BIG_NAMES[index], |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl LateLintPass<'_> for EndianBytes { |
| 110 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 111 | + if in_external_macro(cx.sess(), expr.span) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if_chain! { |
| 116 | + if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind; |
| 117 | + if args.is_empty(); |
| 118 | + let ty = cx.typeck_results().expr_ty(receiver); |
| 119 | + if ty.is_primitive_ty(); |
| 120 | + if maybe_lint_endian_bytes(cx, expr, Prefix::To, method_name.ident.name, ty); |
| 121 | + then { |
| 122 | + return; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if_chain! { |
| 127 | + if let ExprKind::Call(function, ..) = expr.kind; |
| 128 | + if let ExprKind::Path(qpath) = function.kind; |
| 129 | + if let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id(); |
| 130 | + if let Some(function_name) = cx.get_def_path(def_id).last(); |
| 131 | + let ty = cx.typeck_results().expr_ty(expr); |
| 132 | + if ty.is_primitive_ty(); |
| 133 | + then { |
| 134 | + maybe_lint_endian_bytes(cx, expr, Prefix::From, *function_name, ty); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix, name: Symbol, ty: Ty<'_>) -> bool { |
| 141 | + let ne = LintKind::Host.as_name(prefix); |
| 142 | + let le = LintKind::Little.as_name(prefix); |
| 143 | + let be = LintKind::Big.as_name(prefix); |
| 144 | + |
| 145 | + let (lint, other_lints) = match name.as_str() { |
| 146 | + name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]), |
| 147 | + name if name == le => ((&LintKind::Little), [(&LintKind::Host), (&LintKind::Big)]), |
| 148 | + name if name == be => ((&LintKind::Big), [(&LintKind::Host), (&LintKind::Little)]), |
| 149 | + _ => return false, |
| 150 | + }; |
| 151 | + |
| 152 | + let mut help = None; |
| 153 | + |
| 154 | + 'build_help: { |
| 155 | + // all lints disallowed, don't give help here |
| 156 | + if [&[lint], other_lints.as_slice()] |
| 157 | + .concat() |
| 158 | + .iter() |
| 159 | + .all(|lint| !lint.allowed(cx, expr)) |
| 160 | + { |
| 161 | + break 'build_help; |
| 162 | + } |
| 163 | + |
| 164 | + // ne_bytes and all other lints allowed |
| 165 | + if lint.as_name(prefix) == ne && other_lints.iter().all(|lint| lint.allowed(cx, expr)) { |
| 166 | + help = Some(Cow::Borrowed("specify the desired endianness explicitly")); |
| 167 | + break 'build_help; |
| 168 | + } |
| 169 | + |
| 170 | + // le_bytes where ne_bytes allowed but be_bytes is not, or le_bytes where ne_bytes allowed but |
| 171 | + // le_bytes is not |
| 172 | + if (lint.as_name(prefix) == le || lint.as_name(prefix) == be) && LintKind::Host.allowed(cx, expr) { |
| 173 | + help = Some(Cow::Borrowed("use the native endianness instead")); |
| 174 | + break 'build_help; |
| 175 | + } |
| 176 | + |
| 177 | + let allowed_lints = other_lints.iter().filter(|lint| lint.allowed(cx, expr)); |
| 178 | + let len = allowed_lints.clone().count(); |
| 179 | + |
| 180 | + let mut help_str = "use ".to_owned(); |
| 181 | + |
| 182 | + for (i, lint) in allowed_lints.enumerate() { |
| 183 | + let only_one = len == 1; |
| 184 | + if !only_one { |
| 185 | + help_str.push_str("either of "); |
| 186 | + } |
| 187 | + |
| 188 | + help_str.push_str(&format!("`{ty}::{}` ", lint.as_name(prefix))); |
| 189 | + |
| 190 | + if i != len && !only_one { |
| 191 | + help_str.push_str("or "); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + help = Some(Cow::Owned(help_str + "instead")); |
| 196 | + } |
| 197 | + |
| 198 | + span_lint_and_then( |
| 199 | + cx, |
| 200 | + lint.as_lint(), |
| 201 | + expr.span, |
| 202 | + &format!( |
| 203 | + "usage of the {}`{ty}::{}`{}", |
| 204 | + if prefix == Prefix::From { "function " } else { "" }, |
| 205 | + lint.as_name(prefix), |
| 206 | + if prefix == Prefix::To { " method" } else { "" }, |
| 207 | + ), |
| 208 | + move |diag| { |
| 209 | + if let Some(help) = help { |
| 210 | + diag.help(help); |
| 211 | + } |
| 212 | + }, |
| 213 | + ); |
| 214 | + |
| 215 | + true |
| 216 | +} |
0 commit comments