1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT
//! Core analyzer trait and types for code quality analysis.
//!
//! This module defines the fundamental abstractions for building code
//! analyzers:
//! - `Analyzer` trait that all analyzers must implement
//! - `Issue` struct representing detected problems
//! - `AnalysisResult` struct containing analysis outcomes
use Range;
use AppResult;
use File;
/// A single text replacement over the original source.
///
/// Fixes are expressed as byte-range edits against the untouched source text so
/// that everything outside the edited range — comments, blank lines, and the
/// author's formatting — is preserved. This mirrors how `rustfmt` and
/// `rust-analyzer` apply changes, rather than reprinting the AST (which loses
/// comments and reformats the whole file).
///
/// # Examples
///
/// ```
/// use cargo_quality::analyzer::TextEdit;
///
/// let edit = TextEdit {
/// range: 0..9,
/// replacement: String::new()
/// };
/// assert_eq!(edit.range.len(), 9);
/// ```
/// A single fixable change: one source edit plus any import it requires.
///
/// Both the `fix` command and the diff/interactive flow are built from
/// suggestions, so applying a change is identical everywhere: the [`edit`] is
/// spliced into the source and the [`import`], if any, is inserted once
/// (imports are deduplicated across the applied suggestions).
///
/// [`edit`]: Suggestion::edit
/// [`import`]: Suggestion::import
/// Type of fix that can be applied to resolve an issue.
///
/// Represents different kinds of automatic fixes that analyzers can provide.
///
/// # Examples
///
/// ```
/// use cargo_quality::analyzer::Fix;
///
/// let simple_fix = Fix::Simple("let x = 42;".to_string());
/// assert!(simple_fix.is_available());
/// assert_eq!(simple_fix.as_simple(), Some("let x = 42;"));
///
/// let import_fix = Fix::WithImport {
/// import: "use std::fs::read;".to_string(),
/// pattern: "std::fs::read".to_string(),
/// replacement: "read".to_string()
/// };
/// assert!(import_fix.is_available());
/// assert_eq!(
/// import_fix.as_import(),
/// Some(("use std::fs::read;", "std::fs::read", "read"))
/// );
/// ```
/// Analysis issue found in code.
///
/// Represents a single quality issue detected by an analyzer, including
/// its location, description, and optional fix.
///
/// # Examples
///
/// ```
/// # use cargo_quality::analyzer::{Issue, Fix};
/// let issue = Issue {
/// line: 42,
/// column: 15,
/// message: "Use import instead of path".to_string(),
/// fix: Fix::WithImport {
/// import: "use std::fs::read_to_string;".to_string(),
/// pattern: "std::fs::read_to_string".to_string(),
/// replacement: "read_to_string".to_string()
/// }
/// };
/// assert_eq!(issue.line, 42);
/// assert!(issue.fix.is_available());
/// ```
/// Result of code analysis.
///
/// Contains all issues found during analysis and count of fixable issues.
///
/// # Examples
///
/// ```
/// use cargo_quality::analyzer::AnalysisResult;
///
/// let result = AnalysisResult {
/// issues: vec![],
/// fixable_count: 0
/// };
/// assert_eq!(result.issues.len(), 0);
/// ```
/// Trait for code analyzers.
///
/// Implement this trait to create custom quality analyzers. Each analyzer
/// must provide a unique name, analysis logic, and optional fix capability.
///
/// # Examples
///
/// ```
/// use cargo_quality::analyzer::{AnalysisResult, Analyzer};
/// use masterror::AppResult;
/// use syn::File;
///
/// struct MyAnalyzer;
///
/// impl Analyzer for MyAnalyzer {
/// fn name(&self) -> &'static str {
/// "my_analyzer"
/// }
///
/// fn analyze(&self, ast: &File, content: &str) -> AppResult<AnalysisResult> {
/// Ok(AnalysisResult::default())
/// }
/// }
/// ```