forge_lint/sol/info/
pascal_case.rs

1use super::PascalCaseStruct;
2use crate::{
3    linter::{EarlyLintPass, LintContext, Suggestion},
4    sol::{Severity, SolLint},
5};
6use solar::ast::ItemStruct;
7
8declare_forge_lint!(
9    PASCAL_CASE_STRUCT,
10    Severity::Info,
11    "pascal-case-struct",
12    "structs should use PascalCase"
13);
14
15impl<'ast> EarlyLintPass<'ast> for PascalCaseStruct {
16    fn check_item_struct(&mut self, ctx: &LintContext, strukt: &'ast ItemStruct<'ast>) {
17        let name = strukt.name.as_str();
18        if let Some(expected) = check_pascal_case(name) {
19            ctx.emit_with_suggestion(
20                &PASCAL_CASE_STRUCT,
21                strukt.name.span,
22                Suggestion::fix(
23                    expected,
24                    solar::interface::diagnostics::Applicability::MachineApplicable,
25                )
26                .with_desc("consider using"),
27            );
28        }
29    }
30}
31
32/// If the string `s` is not PascalCase, returns a `Some(String)` with the
33/// suggested conversion. Otherwise, returns `None`.
34pub fn check_pascal_case(s: &str) -> Option<String> {
35    if s.len() <= 1 {
36        return None;
37    }
38
39    let expected = heck::AsPascalCase(s).to_string();
40    if s == expected.as_str() { None } else { Some(expected) }
41}