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
use gobject_ast::model::{Expression, Parameter, Statement};
use crate::{
ast_context::AstContext,
config::Config,
rules::{Category, Fix, Rule, Violation},
};
const CHAINABLE_VFUNCS: &[&str] = &["dispose", "finalize", "constructed"];
pub struct GObjectVirtualMethodsChainUp;
impl Rule for GObjectVirtualMethodsChainUp {
fn name(&self) -> &'static str {
"g_object_virtual_methods_chain_up"
}
fn description(&self) -> &'static str {
"Ensure dispose/finalize/constructed methods chain up to parent class"
}
fn category(&self) -> Category {
Category::Correctness
}
fn fixable(&self) -> bool {
true
}
fn check_all(
&self,
ast_context: &AstContext,
config: &Config,
violations: &mut Vec<Violation>,
) {
for (_path, file) in ast_context.iter_all_files() {
for gt in file
.iter_all_gobject_types()
.filter(|gt| gt.kind.is_define())
{
let vfuncs = file.resolve_class_init_vfuncs(gt);
for ((class_type, field), func_name) in &vfuncs {
if class_type != "GObjectClass" || !CHAINABLE_VFUNCS.contains(field) {
continue;
}
let Some(func) = file
.iter_function_definitions()
.find(|f| f.name == *func_name)
else {
continue;
};
if has_chainup_call(&func.body_statements, field) {
continue;
}
let param_name = func
.parameters
.first()
.and_then(|p| match p {
Parameter::Regular { name, .. } => name.as_deref(),
_ => None,
})
.unwrap_or("object");
let parent_class = format!("{}_parent_class", gt.function_prefix);
let cast = config.style.format_call("G_OBJECT_CLASS", &[&parent_class]);
let args = config.style.format_call("", &[param_name]);
let chainup_call = format!("{cast}->{field}{args};");
let fix = if let Some(body_loc) = &func.body_location {
let indent = func
.body_statements
.iter()
.find(|s| {
matches!(s, Statement::Declaration(_) | Statement::Expression(_))
})
.map_or_else(
|| " ".to_string(),
|s| s.location().extract_line_indentation(),
);
let (pos, before, after) = if *field == "constructed" {
let last_decl = func
.body_statements
.iter()
.filter_map(|s| match s {
Statement::Declaration(d) => Some(&d.location),
_ => None,
})
.next_back();
if let Some(loc) = last_decl {
let after = if loc.count_trailing_newlines() >= 2 {
""
} else {
"\n"
};
(loc.end_byte, "\n\n", after)
} else {
(body_loc.start_byte + 1, "\n", "\n")
}
} else {
let trailing = func
.body_statements
.last()
.map_or(1, |s| s.location().count_trailing_newlines());
let before = if trailing >= 2 { "" } else { "\n" };
(body_loc.end_byte - 1, before, "\n")
};
Some(Fix::new(
pos,
pos,
format!("{before}{indent}{chainup_call}{after}"),
))
} else {
None
};
let msg = format!(
"{func_name} must chain up to parent class (e.g., {chainup_call})",
);
let violation = if let Some(fix) = fix {
self.violation_with_fix(
&file.path,
func.location.line,
func.location.column,
msg,
fix,
)
} else {
self.violation(&file.path, func.location.line, func.location.column, msg)
};
violations.push(violation);
}
}
}
}
}
fn has_chainup_call(statements: &[Statement], method_type: &str) -> bool {
for stmt in statements {
let mut found = false;
stmt.walk(&mut |s| {
s.visit_expressions(&mut |expr| {
expr.walk(&mut |e| {
if let Expression::Call(call) = e {
let func = match &*call.function {
Expression::Unary(u) => &u.operand,
other => other,
};
if let Expression::FieldAccess(fa) = func
&& fa.field == method_type
{
found = true;
}
}
});
});
});
if found {
return true;
}
}
false
}