Skip to main content

gobject_linter/rules/
use_g_ascii_functions.rs

1use gobject_ast::model::{FileModel, FunctionDefItem};
2
3use crate::{
4    ast_context::AstContext,
5    config::Config,
6    rules::{FunctionRename, Rule, Violation},
7};
8
9const RENAMES: &[FunctionRename] = &[
10    FunctionRename {
11        from: "tolower",
12        to: Some("g_ascii_tolower"),
13        message: "Use g_ascii_tolower() instead of tolower() — C ctype functions are locale-dependent",
14    },
15    FunctionRename {
16        from: "toupper",
17        to: Some("g_ascii_toupper"),
18        message: "Use g_ascii_toupper() instead of toupper() — C ctype functions are locale-dependent",
19    },
20    FunctionRename {
21        from: "isdigit",
22        to: Some("g_ascii_isdigit"),
23        message: "Use g_ascii_isdigit() instead of isdigit() — C ctype functions are locale-dependent",
24    },
25    FunctionRename {
26        from: "isalpha",
27        to: Some("g_ascii_isalpha"),
28        message: "Use g_ascii_isalpha() instead of isalpha() — C ctype functions are locale-dependent",
29    },
30    FunctionRename {
31        from: "isalnum",
32        to: Some("g_ascii_isalnum"),
33        message: "Use g_ascii_isalnum() instead of isalnum() — C ctype functions are locale-dependent",
34    },
35    FunctionRename {
36        from: "isspace",
37        to: Some("g_ascii_isspace"),
38        message: "Use g_ascii_isspace() instead of isspace() — C ctype functions are locale-dependent",
39    },
40    FunctionRename {
41        from: "isupper",
42        to: Some("g_ascii_isupper"),
43        message: "Use g_ascii_isupper() instead of isupper() — C ctype functions are locale-dependent",
44    },
45    FunctionRename {
46        from: "islower",
47        to: Some("g_ascii_islower"),
48        message: "Use g_ascii_islower() instead of islower() — C ctype functions are locale-dependent",
49    },
50    FunctionRename {
51        from: "isxdigit",
52        to: Some("g_ascii_isxdigit"),
53        message: "Use g_ascii_isxdigit() instead of isxdigit() — C ctype functions are locale-dependent",
54    },
55    FunctionRename {
56        from: "ispunct",
57        to: Some("g_ascii_ispunct"),
58        message: "Use g_ascii_ispunct() instead of ispunct() — C ctype functions are locale-dependent",
59    },
60    FunctionRename {
61        from: "isprint",
62        to: Some("g_ascii_isprint"),
63        message: "Use g_ascii_isprint() instead of isprint() — C ctype functions are locale-dependent",
64    },
65    FunctionRename {
66        from: "isgraph",
67        to: Some("g_ascii_isgraph"),
68        message: "Use g_ascii_isgraph() instead of isgraph() — C ctype functions are locale-dependent",
69    },
70    FunctionRename {
71        from: "iscntrl",
72        to: Some("g_ascii_iscntrl"),
73        message: "Use g_ascii_iscntrl() instead of iscntrl() — C ctype functions are locale-dependent",
74    },
75];
76
77pub struct UseGAsciiFunctions;
78
79impl Rule for UseGAsciiFunctions {
80    fn name(&self) -> &'static str {
81        "use_g_ascii_functions"
82    }
83
84    fn description(&self) -> &'static str {
85        "Use g_ascii_* functions instead of locale-dependent C ctype functions"
86    }
87
88    fn category(&self) -> crate::rules::Category {
89        crate::rules::Category::Correctness
90    }
91
92    fn fixable(&self) -> bool {
93        true
94    }
95
96    fn check_func_impl(
97        &self,
98        _ast_context: &AstContext,
99        config: &Config,
100        func: &FunctionDefItem,
101        file: &FileModel,
102        violations: &mut Vec<Violation>,
103    ) {
104        self.check_function_renames(func, file, config, violations, RENAMES);
105    }
106}