Skip to content

Commit 773bf04

Browse files
authored
fixed npe in UnusedImport check (#507)
* fixed npe in UnusedImport check
1 parent b5c2bba commit 773bf04

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Added related messages to reporters for `CodeSimilarity` check ([#506](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/506))
66
- Fixed Haxe nightly compilation ([#505](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/505))
7+
- Fixed null pointer exception in `UnusedImport` check ([#507](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/507))
78
- Updated to Haxe 4.1.5 ([#505](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/505))
89

910
## version 2.7.0 (2020-12-23)

src/checkstyle/checks/imports/UnusedImportCheck.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class UnusedImportCheck extends Check {
8181
return root.filterCallback(function(token:TokenTree, depth:Int):FilterResult {
8282
return switch (token.tok) {
8383
case Kwd(KwdImport):
84+
if (TokenTreeCheckUtils.isMetadata(token)) {
85+
return SkipSubtree;
86+
}
8487
FoundGoDeeper;
8588
default:
8689
GoDeeper;
@@ -116,6 +119,7 @@ class UnusedImportCheck extends Check {
116119
var moduleName:StringBuf = new StringBuf();
117120

118121
while (true) {
122+
if (token == null) return moduleName.toString();
119123
switch (token.tok) {
120124
case Binop(OpMult):
121125
return null;
@@ -140,6 +144,7 @@ class UnusedImportCheck extends Check {
140144
function detectTypeName(token:TokenTree):String {
141145
var lastName:String = null;
142146
while (true) {
147+
if (token == null) return lastName;
143148
switch (token.tok) {
144149
case Binop(OpMult):
145150
return null;

test/checkstyle/checks/imports/UnusedImportCheckTest.hx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class UnusedImportCheckTest extends CheckTestCase<UnusedImportCheckTests> {
1818
assertNoMsg(check, IMPORT_BASE_CLASS);
1919
assertNoMsg(check, IMPORT_AS);
2020
assertNoMsg(check, IMPORT_IN_STATIC_FUNC);
21+
assertNoMsg(check, METADATA_IMPORT);
2122
}
2223

2324
@Test
@@ -258,4 +259,11 @@ enum abstract UnusedImportCheckTests(String) to String {
258259
trace ('${Check3}');
259260
}
260261
}";
262+
var METADATA_IMPORT = "
263+
abstractAndClass Test {
264+
private static var SRC =
265+
{
266+
@:import h3d.shader.BaseMesh;
267+
};
268+
}";
261269
}

0 commit comments

Comments
 (0)