Skip to content

Commit 7af2433

Browse files
committed
Skip fix with unused self in method call
1 parent 5451778 commit 7af2433

File tree

4 files changed

+13
-47
lines changed

4 files changed

+13
-47
lines changed

selene-lib/src/lints/unused_variable.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
use crate::{
2-
ast_util::{range, scopes::AssignedValue},
2+
ast_util::scopes::AssignedValue,
33
standard_library::{Field, FieldKind, Observes},
44
};
55

66
use super::*;
77

8-
use full_moon::{
9-
ast::Ast,
10-
node::Node,
11-
tokenizer::{Symbol, TokenType},
12-
};
8+
use full_moon::ast::Ast;
139
use regex::Regex;
1410
use serde::Deserialize;
1511

@@ -55,7 +51,7 @@ impl Lint for UnusedVariableLint {
5551
})
5652
}
5753

58-
fn pass(&self, ast: &Ast, context: &Context, ast_context: &AstContext) -> Vec<Diagnostic> {
54+
fn pass(&self, _: &Ast, context: &Context, ast_context: &AstContext) -> Vec<Diagnostic> {
5955
let mut diagnostics = Vec::new();
6056

6157
for (_, variable) in ast_context
@@ -161,8 +157,7 @@ impl Lint for UnusedVariableLint {
161157
{
162158
let mut notes = Vec::new();
163159

164-
let mut variable_range = variable.identifiers[0];
165-
let mut fixed_code = format!("_{}", variable.name);
160+
let mut fixed_code = Some(format!("_{}", variable.name));
166161

167162
if variable.is_self {
168163
if self.allow_unused_self {
@@ -173,24 +168,8 @@ impl Lint for UnusedVariableLint {
173168
notes
174169
.push("if you don't need it, consider using `.` instead of `:`".to_owned());
175170

176-
// This is a very hacky and fragile way to get the colon in the function call. Can we do better?
177-
// We can't just use variable start - 1 due to cases like `function a: b() end`
178-
let mut last_colon_start = 0;
179-
for token in ast.tokens() {
180-
let start_position: usize = range(token).0;
181-
if start_position >= variable_range.0 {
182-
break;
183-
}
184-
185-
if let TokenType::Symbol { symbol } = token.token().token_type() {
186-
if *symbol == Symbol::Colon {
187-
last_colon_start = range(token).0;
188-
}
189-
}
190-
}
191-
192-
variable_range = (last_colon_start, last_colon_start + 1);
193-
fixed_code = ".".to_string();
171+
// Automatically changing `:` to `.` would break any existing methods calls
172+
fixed_code = None;
194173
}
195174

196175
let write_only = !analyzed_references.is_empty();
@@ -202,7 +181,7 @@ impl Lint for UnusedVariableLint {
202181
} else {
203182
format!("{} is defined, but never used", variable.name)
204183
},
205-
Label::new(variable_range),
184+
Label::new(variable.identifiers[0]),
206185
notes,
207186
analyzed_references
208187
.into_iter()
@@ -214,7 +193,7 @@ impl Lint for UnusedVariableLint {
214193
}
215194
})
216195
.collect(),
217-
Some(fixed_code),
196+
fixed_code,
218197
));
219198
};
220199
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
local Foo = {}
22

3-
-function Foo:A() end
4-
-function Foo : B() end
5-
+function Foo.A() end
6-
+function Foo . B() end
7-
function Foo.C() end
3+
function Foo:A() end
4+
function Foo.B() end
85

96
return Foo
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local Foo = {}
22

33
function Foo:A() end
4-
function Foo : B() end
5-
function Foo.C() end
4+
function Foo.B() end
65

76
return Foo

selene-lib/tests/lints/unused_variable/self.stderr

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
error[unused_variable]: self is defined, but never used
2-
┌─ self.lua:3:13
2+
┌─ self.lua:3:14
33
44
3 │ function Foo:A() end
5-
│ ^
6-
7-
= `self` is implicitly defined when defining a method
8-
= if you don't need it, consider using `.` instead of `:`
9-
10-
error[unused_variable]: self is defined, but never used
11-
┌─ self.lua:4:16
12-
13-
4 │ function Foo : B() end
14-
│ ^
5+
│ ^
156
167
= `self` is implicitly defined when defining a method
178
= if you don't need it, consider using `.` instead of `:`

0 commit comments

Comments
 (0)