Skip to content

Commit a057cdf

Browse files
Merge branch 'master' into redundant_local
2 parents 7bd35cb + eacd095 commit a057cdf

31 files changed

+1832
-51
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,6 +4641,7 @@ Released 2018-09-13
46414641
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
46424642
[`await_holding_refcell_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
46434643
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
4644+
[`big_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#big_endian_bytes
46444645
[`bind_instead_of_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
46454646
[`blacklisted_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name
46464647
[`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
48114812
[`get_first`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_first
48124813
[`get_last_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_last_with_len
48134814
[`get_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_unwrap
4815+
[`host_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#host_endian_bytes
48144816
[`identity_conversion`]: https://rust-lang.github.io/rust-clippy/master/index.html#identity_conversion
48154817
[`identity_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
48164818
[`if_let_mutex`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_let_mutex
@@ -4892,6 +4894,7 @@ Released 2018-09-13
48924894
[`let_with_type_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_with_type_underscore
48934895
[`lines_filter_map_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#lines_filter_map_ok
48944896
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
4897+
[`little_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#little_endian_bytes
48954898
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
48964899
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
48974900
[`macro_use_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#macro_use_imports

clippy_lints/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"] }
2222
serde_json = { version = "1.0", optional = true }
2323
tempfile = { version = "3.3.0", optional = true }
2424
toml = "0.7.3"
25+
regex = { version = "1.5", optional = true }
2526
unicode-normalization = "0.1"
2627
unicode-script = { version = "0.5", default-features = false }
2728
semver = "1.0"
@@ -31,7 +32,7 @@ url = "2.2"
3132
[features]
3233
deny-warnings = ["clippy_utils/deny-warnings"]
3334
# build clippy with internal lints enabled, off by default
34-
internal = ["clippy_utils/internal", "serde_json", "tempfile"]
35+
internal = ["clippy_utils/internal", "serde_json", "tempfile", "regex"]
3536

3637
[package.metadata.rust-analyzer]
3738
# This crate uses #[feature(rustc_private)]

clippy_lints/src/allow_attributes.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use ast::AttrStyle;
2-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use ast::{AttrStyle, Attribute};
2+
use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro};
33
use rustc_ast as ast;
44
use rustc_errors::Applicability;
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -50,13 +50,14 @@ declare_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTES]);
5050

5151
impl LateLintPass<'_> for AllowAttribute {
5252
// Separate each crate's features.
53-
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &ast::Attribute) {
53+
fn check_attribute<'cx>(&mut self, cx: &LateContext<'cx>, attr: &'cx Attribute) {
5454
if_chain! {
5555
if !in_external_macro(cx.sess(), attr.span);
5656
if cx.tcx.features().lint_reasons;
5757
if let AttrStyle::Outer = attr.style;
5858
if let Some(ident) = attr.ident();
5959
if ident.name == rustc_span::symbol::sym::allow;
60+
if !is_from_proc_macro(cx, &attr);
6061
then {
6162
span_lint_and_sugg(
6263
cx,

clippy_lints/src/attrs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! checks for attributes
22
3-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
43
use clippy_utils::macros::{is_panic, macro_backtrace};
54
use clippy_utils::msrvs::{self, Msrv};
65
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
6+
use clippy_utils::{
7+
diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then},
8+
is_from_proc_macro,
9+
};
710
use if_chain::if_chain;
811
use rustc_ast::{AttrKind, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem};
912
use rustc_errors::Applicability;
@@ -566,7 +569,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, name: Symbol, items: &[NestedMe
566569
}
567570
}
568571

569-
fn check_lint_reason(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem], attr: &'_ Attribute) {
572+
fn check_lint_reason<'cx>(cx: &LateContext<'cx>, name: Symbol, items: &[NestedMetaItem], attr: &'cx Attribute) {
570573
// Check for the feature
571574
if !cx.tcx.features().lint_reasons {
572575
return;
@@ -581,7 +584,7 @@ fn check_lint_reason(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem
581584
}
582585

583586
// Check if the attribute is in an external macro and therefore out of the developer's control
584-
if in_external_macro(cx.sess(), attr.span) {
587+
if in_external_macro(cx.sess(), attr.span) || is_from_proc_macro(cx, &attr) {
585588
return;
586589
}
587590

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ declare_clippy_lint! {
522522

523523
declare_clippy_lint! {
524524
/// ### What it does
525-
/// Check for the usage of `as _` conversion using inferred type.
525+
/// Checks for the usage of `as _` conversion using inferred type.
526526
///
527527
/// ### Why is this bad?
528528
/// The conversion might include lossy conversion and dangerous cast that might go

clippy_lints/src/declared_lints.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Manual edits will be overwritten.
44

55
pub(crate) static LINTS: &[&crate::LintInfo] = &[
6+
#[cfg(feature = "internal")]
7+
crate::utils::internal_lints::almost_standard_lint_formulation::ALMOST_STANDARD_LINT_FORMULATION_INFO,
68
#[cfg(feature = "internal")]
79
crate::utils::internal_lints::clippy_lints_internal::CLIPPY_LINTS_INTERNAL_INFO,
810
#[cfg(feature = "internal")]
@@ -143,6 +145,9 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
143145
crate::empty_drop::EMPTY_DROP_INFO,
144146
crate::empty_enum::EMPTY_ENUM_INFO,
145147
crate::empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS_INFO,
148+
crate::endian_bytes::BIG_ENDIAN_BYTES_INFO,
149+
crate::endian_bytes::HOST_ENDIAN_BYTES_INFO,
150+
crate::endian_bytes::LITTLE_ENDIAN_BYTES_INFO,
146151
crate::entry::MAP_ENTRY_INFO,
147152
crate::enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT_INFO,
148153
crate::enum_variants::ENUM_VARIANT_NAMES_INFO,

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
88

99
declare_clippy_lint! {
1010
/// ### What it does
11-
/// Check for construction on unit struct using `default`.
11+
/// Checks for construction on unit struct using `default`.
1212
///
1313
/// ### Why is this bad?
1414
/// This adds code complexity and an unnecessary function call.

clippy_lints/src/dereference.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ declare_clippy_lint! {
5656
/// let b = &*a;
5757
/// ```
5858
///
59-
/// This lint excludes:
59+
/// This lint excludes all of:
6060
/// ```rust,ignore
6161
/// let _ = d.unwrap().deref();
62+
/// let _ = Foo::deref(&foo);
63+
/// let _ = <Foo as Deref>::deref(&foo);
6264
/// ```
6365
#[clippy::version = "1.44.0"]
6466
pub EXPLICIT_DEREF_METHODS,
@@ -1480,7 +1482,7 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
14801482
target_mut,
14811483
} => {
14821484
let mut app = Applicability::MachineApplicable;
1483-
let (expr_str, expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
1485+
let (expr_str, _expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
14841486
let ty = cx.typeck_results().expr_ty(expr);
14851487
let (_, ref_count) = peel_mid_ty_refs(ty);
14861488
let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
@@ -1503,11 +1505,20 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
15031505
"&"
15041506
};
15051507

1506-
let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
1507-
format!("({expr_str})")
1508+
// expr_str (the suggestion) is never shown if is_final_ufcs is true, since it's
1509+
// `expr.kind == ExprKind::Call`. Therefore, this is, afaik, always unnecessary.
1510+
/*
1511+
expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
1512+
Cow::Owned(format!("({expr_str})"))
15081513
} else {
1509-
expr_str.into_owned()
1514+
expr_str
15101515
};
1516+
*/
1517+
1518+
// Fix #10850, do not lint if it's `Foo::deref` instead of `foo.deref()`.
1519+
if is_final_ufcs {
1520+
return;
1521+
}
15111522

15121523
span_lint_and_sugg(
15131524
cx,

clippy_lints/src/endian_bytes.rs

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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

Comments
 (0)