@@ -3,10 +3,11 @@ use clippy_utils::diagnostics::span_lint_and_then;
3
3
use clippy_utils:: macros:: root_macro_call_first_node;
4
4
use clippy_utils:: source:: snippet_opt;
5
5
use rustc_ast:: LitKind ;
6
- use rustc_hir:: { AttrArgs , AttrKind , Attribute , Expr , ExprKind } ;
7
- use rustc_lint:: { LateContext , LateLintPass } ;
6
+ use rustc_hir:: { Expr , ExprKind } ;
7
+ use rustc_ast:: { Attribute , AttrArgs , AttrKind } ;
8
+ use rustc_lint:: { EarlyContext , EarlyLintPass , LateContext , LateLintPass } ;
8
9
use rustc_session:: impl_lint_pass;
9
- use rustc_span:: { Span , sym} ;
10
+ use rustc_span:: sym;
10
11
11
12
declare_clippy_lint ! {
12
13
/// ### What it does
@@ -52,24 +53,6 @@ impl LargeIncludeFile {
52
53
53
54
impl_lint_pass ! ( LargeIncludeFile => [ LARGE_INCLUDE_FILE ] ) ;
54
55
55
- impl LargeIncludeFile {
56
- fn emit_lint ( & self , cx : & LateContext < ' _ > , span : Span ) {
57
- #[ expect( clippy:: collapsible_span_lint_calls, reason = "rust-clippy#7797" ) ]
58
- span_lint_and_then (
59
- cx,
60
- LARGE_INCLUDE_FILE ,
61
- span,
62
- "attempted to include a large file" ,
63
- |diag| {
64
- diag. note ( format ! (
65
- "the configuration allows a maximum size of {} bytes" ,
66
- self . max_file_size
67
- ) ) ;
68
- } ,
69
- ) ;
70
- }
71
- }
72
-
73
56
impl LateLintPass < ' _ > for LargeIncludeFile {
74
57
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > ) {
75
58
if let ExprKind :: Lit ( lit) = & expr. kind
@@ -85,18 +68,32 @@ impl LateLintPass<'_> for LargeIncludeFile {
85
68
&& ( cx. tcx . is_diagnostic_item ( sym:: include_bytes_macro, macro_call. def_id )
86
69
|| cx. tcx . is_diagnostic_item ( sym:: include_str_macro, macro_call. def_id ) )
87
70
{
88
- self . emit_lint ( cx, expr. span . source_callsite ( ) ) ;
71
+ #[ expect( clippy:: collapsible_span_lint_calls, reason = "rust-clippy#7797" ) ]
72
+ span_lint_and_then (
73
+ cx,
74
+ LARGE_INCLUDE_FILE ,
75
+ expr. span . source_callsite ( ) ,
76
+ "attempted to include a large file" ,
77
+ |diag| {
78
+ diag. note ( format ! (
79
+ "the configuration allows a maximum size of {} bytes" ,
80
+ self . max_file_size
81
+ ) ) ;
82
+ } ,
83
+ ) ;
89
84
}
90
85
}
86
+ }
91
87
92
- fn check_attribute ( & mut self , cx : & LateContext < ' _ > , attr : & Attribute ) {
88
+ impl EarlyLintPass for LargeIncludeFile {
89
+ fn check_attribute ( & mut self , cx : & EarlyContext < ' _ > , attr : & Attribute ) {
93
90
if !attr. span . from_expansion ( )
94
91
// Currently, rustc limits the usage of macro at the top-level of attributes,
95
92
// so we don't need to recurse into each level.
96
93
&& let AttrKind :: Normal ( ref item) = attr. kind
97
94
&& let Some ( doc) = attr. doc_str ( )
98
95
&& doc. as_str ( ) . len ( ) as u64 > self . max_file_size
99
- && let AttrArgs :: Eq { expr : meta, .. } = & item. args
96
+ && let AttrArgs :: Eq { expr : meta, .. } = & item. item . args
100
97
&& !attr. span . contains ( meta. span )
101
98
// Since the `include_str` is already expanded at this point, we can only take the
102
99
// whole attribute snippet and then modify for our suggestion.
@@ -113,7 +110,19 @@ impl LateLintPass<'_> for LargeIncludeFile {
113
110
&& let sub_snippet = sub_snippet. trim ( )
114
111
&& ( sub_snippet. starts_with ( "include_str!" ) || sub_snippet. starts_with ( "include_bytes!" ) )
115
112
{
116
- self . emit_lint ( cx, attr. span ) ;
113
+ #[ expect( clippy:: collapsible_span_lint_calls, reason = "rust-clippy#7797" ) ]
114
+ span_lint_and_then (
115
+ cx,
116
+ LARGE_INCLUDE_FILE ,
117
+ attr. span ,
118
+ "attempted to include a large file" ,
119
+ |diag| {
120
+ diag. note ( format ! (
121
+ "the configuration allows a maximum size of {} bytes" ,
122
+ self . max_file_size
123
+ ) ) ;
124
+ } ,
125
+ ) ;
117
126
}
118
127
}
119
128
}
0 commit comments