vibe-style 0.1.17

Rust style checker with syntax and semantic analysis, plus a safe auto-fixer for deterministic, rule-driven code layout.
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::collections::BTreeMap;

use ra_ap_syntax::{
	AstNode, SyntaxKind,
	ast::{
		self, GenericArg, GenericParam, HasGenericArgs, HasGenericParams, HasName, HasVisibility,
		Path, PathSegment, Type, TypeAlias, Use,
	},
};

use crate::style::shared::{self, Edit, FileContext, Violation};

const RULE_ID: &str = "RUST-STYLE-TYPE-001";
const MESSAGE: &str = "Do not use type aliases that only rename another type.";

enum AliasGenericParam {
	Lifetime(String),
	Type(String),
}

#[derive(Clone, Debug)]
pub(crate) struct TypeAliasRenameFix {
	pub(crate) alias: String,
	pub(crate) target: String,
	pub(crate) definition_edits: Vec<Edit>,
}

pub(crate) fn check_type_alias_renames(ctx: &FileContext, violations: &mut Vec<Violation>) {
	for type_alias in ctx.source_file.syntax().descendants().filter_map(TypeAlias::cast) {
		if !is_meaningless_type_alias_item(&type_alias) {
			continue;
		}

		let Some(Type::PathType(path_type)) = type_alias.ty() else {
			continue;
		};
		let Some(aliases) = alias_generic_keys(&type_alias) else {
			continue;
		};
		let Some(rhs_path) = path_type.path() else {
			continue;
		};

		if !is_meaningless_alias(&rhs_path, &aliases) {
			continue;
		}

		let start = usize::from(path_type.syntax().text_range().start());
		let line = shared::line_from_offset(&ctx.line_starts, start);
		let fixable = type_alias_autofix_plan(ctx, &type_alias, &rhs_path, &aliases).is_some();

		shared::push_violation(violations, ctx, line, RULE_ID, MESSAGE, fixable);
	}
}

pub(crate) fn collect_type_alias_rename_fixes(ctx: &FileContext) -> Vec<TypeAliasRenameFix> {
	let mut out = Vec::new();

	for type_alias in ctx.source_file.syntax().descendants().filter_map(TypeAlias::cast) {
		if !is_meaningless_type_alias_item(&type_alias) {
			continue;
		}

		let Some(Type::PathType(path_type)) = type_alias.ty() else {
			continue;
		};
		let Some(aliases) = alias_generic_keys(&type_alias) else {
			continue;
		};
		let Some(rhs_path) = path_type.path() else {
			continue;
		};
		let Some(plan) = type_alias_autofix_plan(ctx, &type_alias, &rhs_path, &aliases) else {
			continue;
		};

		out.push(plan);
	}

	out
}

pub(crate) fn build_type_alias_usage_rename_edits(
	ctx: &FileContext,
	renames: &BTreeMap<String, String>,
	skip_ranges: &[(usize, usize)],
) -> Vec<Edit> {
	if renames.is_empty() {
		return Vec::new();
	}

	let mut edits = Vec::new();

	for segment in ctx.source_file.syntax().descendants().filter_map(PathSegment::cast) {
		let Some(name_ref) = segment.name_ref() else {
			continue;
		};
		let Some(replacement) = renames.get(name_ref.text().as_str()) else {
			continue;
		};
		let range = name_ref.syntax().text_range();
		let start = usize::from(range.start());
		let end = usize::from(range.end());

		if skip_ranges.iter().any(|(skip_start, skip_end)| start < *skip_end && end > *skip_start) {
			continue;
		}

		edits.push(Edit { start, end, replacement: replacement.clone(), rule: RULE_ID });
	}

	edits
}

fn is_meaningless_type_alias_item(type_alias: &TypeAlias) -> bool {
	let mut ancestor = type_alias.syntax().parent();

	while let Some(node) = ancestor {
		match node.kind() {
			SyntaxKind::ASSOC_ITEM_LIST => return false,
			SyntaxKind::ITEM_LIST | SyntaxKind::SOURCE_FILE | SyntaxKind::BLOCK_EXPR => {
				return true;
			},
			_ => {},
		}

		ancestor = node.parent();
	}

	true
}

fn is_meaningless_alias(path: &Path, aliases: &[AliasGenericParam]) -> bool {
	let mut segments = Vec::<PathSegment>::new();

	if !collect_simple_path_segments(path, &mut segments) {
		return false;
	}
	if segments.is_empty() {
		return false;
	}

	for segment in segments.iter().take(segments.len() - 1) {
		if segment.generic_arg_list().is_some() {
			return false;
		}
	}

	let Some(last_segment) = segments.last() else {
		return false;
	};
	let Some(last_generic_args) = last_segment.generic_arg_list() else {
		return aliases.is_empty();
	};
	let rhs_args = last_generic_args.generic_args().collect::<Vec<GenericArg>>();

	if rhs_args.len() != aliases.len() {
		return false;
	}

	for (rhs_arg, alias) in rhs_args.iter().zip(aliases) {
		if !generic_arg_matches_param(rhs_arg, alias) {
			return false;
		}
	}

	true
}

fn type_alias_autofix_plan(
	ctx: &FileContext,
	type_alias: &TypeAlias,
	rhs_path: &Path,
	aliases: &[AliasGenericParam],
) -> Option<TypeAliasRenameFix> {
	if !is_meaningless_alias(rhs_path, aliases) {
		return None;
	}

	let alias_name = type_alias.name()?.text().to_string();
	let mut rhs_segments = Vec::<PathSegment>::new();

	if !collect_simple_path_segments(rhs_path, &mut rhs_segments) || rhs_segments.is_empty() {
		return None;
	}

	let target_segment = rhs_segments.last()?;
	let target_name = target_segment.name_ref()?.text().to_string();
	let alias_is_public = type_alias.visibility().is_some();
	let alias_start = usize::from(type_alias.syntax().text_range().start());
	let alias_end = usize::from(type_alias.syntax().text_range().end());
	let mut definition_edits = Vec::new();

	if alias_is_public {
		if rhs_segments.len() >= 2 {
			let rhs_text = rhs_path.syntax().text().to_string();

			definition_edits.push(Edit {
				start: alias_start,
				end: alias_end,
				replacement: format!("pub use {rhs_text};"),
				rule: RULE_ID,
			});
		} else if let Some((use_start, use_end, use_path, use_is_pub)) =
			find_simple_sibling_use_importing_ident(ctx, type_alias, target_name.as_str())
		{
			if use_is_pub {
				// The target is already exported; remove the alias and rewrite callers.
				definition_edits.push(Edit {
					start: alias_start,
					end: alias_end,
					replacement: String::new(),
					rule: RULE_ID,
				});
			} else {
				definition_edits.push(Edit {
					start: alias_start,
					end: alias_end,
					replacement: format!("pub use {use_path};"),
					rule: RULE_ID,
				});
				definition_edits.push(Edit {
					start: use_start,
					end: use_end,
					replacement: String::new(),
					rule: RULE_ID,
				});
			}
		} else if is_primitive_type_ident(&target_name) {
			definition_edits.push(Edit {
				start: alias_start,
				end: alias_end,
				replacement: String::new(),
				rule: RULE_ID,
			});
		} else {
			return None;
		}
	} else {
		definition_edits.push(Edit {
			start: alias_start,
			end: alias_end,
			replacement: String::new(),
			rule: RULE_ID,
		});
	}

	Some(TypeAliasRenameFix { alias: alias_name, target: target_name, definition_edits })
}

fn is_primitive_type_ident(name: &str) -> bool {
	matches!(
		name,
		"bool"
			| "char" | "str"
			| "i8" | "i16"
			| "i32" | "i64"
			| "i128" | "isize"
			| "u8" | "u16"
			| "u32" | "u64"
			| "u128" | "usize"
			| "f32" | "f64"
	)
}

fn simple_use_path_text(text: &str) -> Option<String> {
	let text = text.trim();
	let start = text.find("use")?;
	let after = text.get(start + 3..)?;
	let bytes = after.as_bytes();
	let mut idx = 0_usize;

	while idx < bytes.len() && bytes[idx].is_ascii_whitespace() {
		idx += 1;
	}

	let tail = after.get(idx..)?;
	let semi = tail.find(';')?;
	let use_path = tail[..semi].trim();

	if use_path.is_empty()
		|| use_path.contains('{')
		|| use_path.contains('}')
		|| use_path.contains('*')
		|| use_path.contains(" as ")
	{
		return None;
	}

	Some(use_path.to_string())
}

fn find_simple_sibling_use_importing_ident(
	ctx: &FileContext,
	type_alias: &TypeAlias,
	ident: &str,
) -> Option<(usize, usize, String, bool)> {
	let parent = type_alias.syntax().parent()?;

	for use_item in ctx.source_file.syntax().descendants().filter_map(Use::cast) {
		if use_item.syntax().parent() != Some(parent.clone()) {
			continue;
		}

		let path_text = simple_use_path_text(&use_item.syntax().text().to_string())?;
		let last = path_text.rsplit("::").next().unwrap_or(path_text.as_str()).trim();

		if last != ident {
			continue;
		}

		let start = usize::from(use_item.syntax().text_range().start());
		let end = usize::from(use_item.syntax().text_range().end());
		let is_pub = use_item.visibility().is_some();

		return Some((start, end, path_text, is_pub));
	}

	None
}

fn generic_arg_matches_param(arg: &GenericArg, alias: &AliasGenericParam) -> bool {
	match (arg, alias) {
		(ast::GenericArg::LifetimeArg(lifetime_arg), AliasGenericParam::Lifetime(expected)) => {
			let Some(lifetime) = lifetime_arg.lifetime() else {
				return false;
			};
			let Some(token) = lifetime.lifetime_ident_token() else {
				return false;
			};

			token.text() == expected.as_str()
		},
		(ast::GenericArg::TypeArg(type_arg), AliasGenericParam::Type(expected)) => {
			let Some(type_arg_type) = type_arg.ty() else {
				return false;
			};
			let Type::PathType(path_type) = type_arg_type else {
				return false;
			};
			let Some(path) = path_type.path() else {
				return false;
			};
			let mut segments = Vec::<PathSegment>::new();

			if !collect_simple_path_segments(&path, &mut segments) {
				return false;
			}
			if segments.len() != 1 {
				return false;
			}

			let Some(name_ref) = segments[0].name_ref() else {
				return false;
			};

			name_ref.text() == expected.as_str()
		},
		_ => false,
	}
}

fn alias_generic_keys(type_alias: &TypeAlias) -> Option<Vec<AliasGenericParam>> {
	let Some(generic_params) = type_alias.generic_param_list() else {
		return Some(Vec::new());
	};
	let mut out = Vec::new();

	for param in generic_params.generic_params() {
		match param {
			GenericParam::TypeParam(type_param) => {
				// A default generic parameter (for example `E = Error`) changes the alias API
				// surface, so it is not considered a pure rename.
				if type_param
					.syntax()
					.children_with_tokens()
					.any(|token| token.kind() == SyntaxKind::EQ)
				{
					return None;
				}

				let name = type_param.name()?;

				out.push(AliasGenericParam::Type(name.text().to_string()));
			},
			GenericParam::LifetimeParam(lifetime_param) => {
				let lifetime = lifetime_param.lifetime()?;
				let token = lifetime.lifetime_ident_token()?;

				out.push(AliasGenericParam::Lifetime(token.text().to_string()));
			},
			GenericParam::ConstParam(_) => return None,
		}
	}

	Some(out)
}

fn collect_simple_path_segments(path: &Path, out: &mut Vec<PathSegment>) -> bool {
	if let Some(qualifier) = path.qualifier()
		&& !collect_simple_path_segments(&qualifier, out)
	{
		return false;
	}

	let Some(segment) = path.segment() else {
		return false;
	};

	if segment.type_anchor().is_some()
		|| segment.parenthesized_arg_list().is_some()
		|| segment.ret_type().is_some()
		|| segment.return_type_syntax().is_some()
	{
		return false;
	}

	out.push(segment);

	true
}