@@ -2,8 +2,11 @@ use std::fmt;
22
33use clippy_utils:: diagnostics:: span_lint_and_help;
44use rustc_ast:: ast:: { Expr , ExprKind , InlineAsmOptions } ;
5- use rustc_lint:: { EarlyContext , EarlyLintPass , Lint } ;
5+ use rustc_ast:: { InlineAsm , Item , ItemKind } ;
6+ use rustc_lint:: { EarlyContext , EarlyLintPass , Lint , LintContext } ;
67use rustc_session:: declare_lint_pass;
8+ use rustc_span:: Span ;
9+ use rustc_target:: asm:: InlineAsmArch ;
710
811#[ derive( Clone , Copy , PartialEq , Eq ) ]
912enum AsmStyle {
@@ -31,8 +34,14 @@ impl std::ops::Not for AsmStyle {
3134 }
3235}
3336
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 {
37+ fn check_asm_syntax (
38+ lint : & ' static Lint ,
39+ cx : & EarlyContext < ' _ > ,
40+ inline_asm : & InlineAsm ,
41+ span : Span ,
42+ check_for : AsmStyle ,
43+ ) {
44+ if matches ! ( cx. sess( ) . asm_arch, Some ( InlineAsmArch :: X86 | InlineAsmArch :: X86_64 ) ) {
3645 let style = if inline_asm. options . contains ( InlineAsmOptions :: ATT_SYNTAX ) {
3746 AsmStyle :: Att
3847 } else {
@@ -43,7 +52,7 @@ fn check_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr
4352 span_lint_and_help (
4453 cx,
4554 lint,
46- expr . span ,
55+ span,
4756 & format ! ( "{style} x86 assembly syntax used" ) ,
4857 None ,
4958 & format ! ( "use {} x86 assembly syntax" , !style) ,
@@ -89,7 +98,15 @@ declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
8998
9099impl EarlyLintPass for InlineAsmX86IntelSyntax {
91100 fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & Expr ) {
92- check_expr_asm_syntax ( Self :: get_lints ( ) [ 0 ] , cx, expr, AsmStyle :: Intel ) ;
101+ if let ExprKind :: InlineAsm ( inline_asm) = & expr. kind {
102+ check_asm_syntax ( Self :: get_lints ( ) [ 0 ] , cx, inline_asm, expr. span , AsmStyle :: Intel ) ;
103+ }
104+ }
105+
106+ fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
107+ if let ItemKind :: GlobalAsm ( inline_asm) = & item. kind {
108+ check_asm_syntax ( Self :: get_lints ( ) [ 0 ] , cx, inline_asm, item. span , AsmStyle :: Intel ) ;
109+ }
93110 }
94111}
95112
@@ -130,6 +147,14 @@ declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
130147
131148impl EarlyLintPass for InlineAsmX86AttSyntax {
132149 fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & Expr ) {
133- check_expr_asm_syntax ( Self :: get_lints ( ) [ 0 ] , cx, expr, AsmStyle :: Att ) ;
150+ if let ExprKind :: InlineAsm ( inline_asm) = & expr. kind {
151+ check_asm_syntax ( Self :: get_lints ( ) [ 0 ] , cx, inline_asm, expr. span , AsmStyle :: Att ) ;
152+ }
153+ }
154+
155+ fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
156+ if let ItemKind :: GlobalAsm ( inline_asm) = & item. kind {
157+ check_asm_syntax ( Self :: get_lints ( ) [ 0 ] , cx, inline_asm, item. span , AsmStyle :: Att ) ;
158+ }
134159 }
135160}
0 commit comments