Skip to content

Commit b1205bf

Browse files
committed
Merge remote-tracking branch 'upstream/master' into unnecessary_wrap
2 parents 5740af7 + f0eac45 commit b1205bf

36 files changed

+1166
-122
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,7 @@ Released 2018-09-13
15591559
[`deref_addrof`]: https://rust-lang.github.io/rust-clippy/master/index.html#deref_addrof
15601560
[`derive_hash_xor_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
15611561
[`derive_ord_xor_partial_ord`]: https://rust-lang.github.io/rust-clippy/master/index.html#derive_ord_xor_partial_ord
1562+
[`disallowed_method`]: https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_method
15621563
[`diverging_sub_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#diverging_sub_expression
15631564
[`doc_markdown`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
15641565
[`double_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_comparisons
@@ -1634,6 +1635,8 @@ Released 2018-09-13
16341635
[`inherent_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
16351636
[`inherent_to_string_shadow_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display
16361637
[`inline_always`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_always
1638+
[`inline_asm_x86_att_syntax`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_asm_x86_att_syntax
1639+
[`inline_asm_x86_intel_syntax`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_asm_x86_intel_syntax
16371640
[`inline_fn_without_body`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_fn_without_body
16381641
[`int_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#int_plus_one
16391642
[`integer_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic

clippy_lints/src/asm_syntax.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use std::fmt;
2+
3+
use crate::utils::span_lint_and_help;
4+
use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions};
5+
use rustc_lint::{EarlyContext, EarlyLintPass, Lint};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
8+
#[derive(Clone, Copy, PartialEq, Eq)]
9+
enum AsmStyle {
10+
Intel,
11+
Att,
12+
}
13+
14+
impl fmt::Display for AsmStyle {
15+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16+
match self {
17+
AsmStyle::Intel => f.write_str("Intel"),
18+
AsmStyle::Att => f.write_str("AT&T"),
19+
}
20+
}
21+
}
22+
23+
impl std::ops::Not for AsmStyle {
24+
type Output = AsmStyle;
25+
26+
fn not(self) -> AsmStyle {
27+
match self {
28+
AsmStyle::Intel => AsmStyle::Att,
29+
AsmStyle::Att => AsmStyle::Intel,
30+
}
31+
}
32+
}
33+
34+
fn check_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr, check_for: AsmStyle) {
35+
if let ExprKind::InlineAsm(ref inline_asm) = expr.kind {
36+
let style = if inline_asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
37+
AsmStyle::Att
38+
} else {
39+
AsmStyle::Intel
40+
};
41+
42+
if style == check_for {
43+
span_lint_and_help(
44+
cx,
45+
lint,
46+
expr.span,
47+
&format!("{} x86 assembly syntax used", style),
48+
None,
49+
&format!("use {} x86 assembly syntax", !style),
50+
);
51+
}
52+
}
53+
}
54+
55+
declare_clippy_lint! {
56+
/// **What it does:** Checks for usage of Intel x86 assembly syntax.
57+
///
58+
/// **Why is this bad?** The lint has been enabled to indicate a preference
59+
/// for AT&T x86 assembly syntax.
60+
///
61+
/// **Known problems:** None.
62+
///
63+
/// **Example:**
64+
///
65+
/// ```rust,no_run
66+
/// # #![feature(asm)]
67+
/// # unsafe { let ptr = "".as_ptr();
68+
/// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr);
69+
/// # }
70+
/// ```
71+
/// Use instead:
72+
/// ```rust,no_run
73+
/// # #![feature(asm)]
74+
/// # unsafe { let ptr = "".as_ptr();
75+
/// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax));
76+
/// # }
77+
/// ```
78+
pub INLINE_ASM_X86_INTEL_SYNTAX,
79+
restriction,
80+
"prefer AT&T x86 assembly syntax"
81+
}
82+
83+
declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
84+
85+
impl EarlyLintPass for InlineAsmX86IntelSyntax {
86+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
87+
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Intel);
88+
}
89+
}
90+
91+
declare_clippy_lint! {
92+
/// **What it does:** Checks for usage of AT&T x86 assembly syntax.
93+
///
94+
/// **Why is this bad?** The lint has been enabled to indicate a preference
95+
/// for Intel x86 assembly syntax.
96+
///
97+
/// **Known problems:** None.
98+
///
99+
/// **Example:**
100+
///
101+
/// ```rust,no_run
102+
/// # #![feature(asm)]
103+
/// # unsafe { let ptr = "".as_ptr();
104+
/// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax));
105+
/// # }
106+
/// ```
107+
/// Use instead:
108+
/// ```rust,no_run
109+
/// # #![feature(asm)]
110+
/// # unsafe { let ptr = "".as_ptr();
111+
/// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr);
112+
/// # }
113+
/// ```
114+
pub INLINE_ASM_X86_ATT_SYNTAX,
115+
restriction,
116+
"prefer Intel x86 assembly syntax"
117+
}
118+
119+
declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
120+
121+
impl EarlyLintPass for InlineAsmX86AttSyntax {
122+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
123+
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Att);
124+
}
125+
}

clippy_lints/src/disallowed_method.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use crate::utils::span_lint;
2+
3+
use rustc_data_structures::fx::FxHashSet;
4+
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::{declare_tool_lint, impl_lint_pass};
7+
use rustc_span::Symbol;
8+
9+
declare_clippy_lint! {
10+
/// **What it does:** Lints for specific trait methods defined in clippy.toml
11+
///
12+
/// **Why is this bad?** Some methods are undesirable in certain contexts,
13+
/// and it would be beneficial to lint for them as needed.
14+
///
15+
/// **Known problems:** None.
16+
///
17+
/// **Example:**
18+
///
19+
/// ```rust,ignore
20+
/// // example code where clippy issues a warning
21+
/// foo.bad_method(); // Foo::bad_method is disallowed in the configuration
22+
/// ```
23+
/// Use instead:
24+
/// ```rust,ignore
25+
/// // example code which does not raise clippy warning
26+
/// goodStruct.bad_method(); // GoodStruct::bad_method is not disallowed
27+
/// ```
28+
pub DISALLOWED_METHOD,
29+
nursery,
30+
"use of a disallowed method call"
31+
}
32+
33+
#[derive(Clone, Debug)]
34+
pub struct DisallowedMethod {
35+
disallowed: FxHashSet<Vec<Symbol>>,
36+
}
37+
38+
impl DisallowedMethod {
39+
pub fn new(disallowed: &FxHashSet<String>) -> Self {
40+
Self {
41+
disallowed: disallowed
42+
.iter()
43+
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
44+
.collect(),
45+
}
46+
}
47+
}
48+
49+
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
50+
51+
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
52+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
53+
if let ExprKind::MethodCall(_path, _, _args, _) = &expr.kind {
54+
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
55+
56+
let method_call = cx.get_def_path(def_id);
57+
if self.disallowed.contains(&method_call) {
58+
let method = method_call
59+
.iter()
60+
.map(|s| s.to_ident_string())
61+
.collect::<Vec<_>>()
62+
.join("::");
63+
64+
span_lint(
65+
cx,
66+
DISALLOWED_METHOD,
67+
expr.span,
68+
&format!("use of a disallowed method `{}`", method),
69+
);
70+
}
71+
}
72+
}
73+
}

clippy_lints/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(concat_idents)]
77
#![feature(crate_visibility_modifier)]
88
#![feature(drain_filter)]
9+
#![feature(in_band_lifetimes)]
910
#![feature(or_patterns)]
1011
#![feature(rustc_private)]
1112
#![feature(stmt_expr_attributes)]
@@ -152,6 +153,7 @@ mod utils;
152153
mod approx_const;
153154
mod arithmetic;
154155
mod as_conversions;
156+
mod asm_syntax;
155157
mod assertions_on_constants;
156158
mod assign_ops;
157159
mod async_yields_async;
@@ -175,6 +177,7 @@ mod dbg_macro;
175177
mod default_trait_access;
176178
mod dereference;
177179
mod derive;
180+
mod disallowed_method;
178181
mod doc;
179182
mod double_comparison;
180183
mod double_parens;
@@ -486,6 +489,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
486489
&arithmetic::FLOAT_ARITHMETIC,
487490
&arithmetic::INTEGER_ARITHMETIC,
488491
&as_conversions::AS_CONVERSIONS,
492+
&asm_syntax::INLINE_ASM_X86_ATT_SYNTAX,
493+
&asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX,
489494
&assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
490495
&assign_ops::ASSIGN_OP_PATTERN,
491496
&assign_ops::MISREFACTORED_ASSIGN_OP,
@@ -526,6 +531,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
526531
&derive::DERIVE_ORD_XOR_PARTIAL_ORD,
527532
&derive::EXPL_IMPL_CLONE_ON_COPY,
528533
&derive::UNSAFE_DERIVE_DESERIALIZE,
534+
&disallowed_method::DISALLOWED_METHOD,
529535
&doc::DOC_MARKDOWN,
530536
&doc::MISSING_ERRORS_DOC,
531537
&doc::MISSING_SAFETY_DOC,
@@ -1121,11 +1127,18 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11211127
store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync);
11221128
store.register_late_pass(|| box manual_strip::ManualStrip);
11231129
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
1130+
let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::<FxHashSet<_>>();
1131+
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));
1132+
store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax);
1133+
store.register_early_pass(|| box asm_syntax::InlineAsmX86IntelSyntax);
1134+
11241135

11251136
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
11261137
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
11271138
LintId::of(&arithmetic::INTEGER_ARITHMETIC),
11281139
LintId::of(&as_conversions::AS_CONVERSIONS),
1140+
LintId::of(&asm_syntax::INLINE_ASM_X86_ATT_SYNTAX),
1141+
LintId::of(&asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX),
11291142
LintId::of(&create_dir::CREATE_DIR),
11301143
LintId::of(&dbg_macro::DBG_MACRO),
11311144
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),
@@ -1812,6 +1825,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18121825
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
18131826
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
18141827
LintId::of(&cognitive_complexity::COGNITIVE_COMPLEXITY),
1828+
LintId::of(&disallowed_method::DISALLOWED_METHOD),
18151829
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
18161830
LintId::of(&floating_point_arithmetic::IMPRECISE_FLOPS),
18171831
LintId::of(&floating_point_arithmetic::SUBOPTIMAL_FLOPS),

0 commit comments

Comments
 (0)