Skip to content

Commit

Permalink
fixed npe in UnusedImport check (#507)
Browse files Browse the repository at this point in the history
* fixed npe in UnusedImport check
  • Loading branch information
AlexHaxe authored May 12, 2021
1 parent b5c2bba commit 773bf04
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

## version 2.7.0 (2020-12-23)
Expand Down
5 changes: 5 additions & 0 deletions src/checkstyle/checks/imports/UnusedImportCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class UnusedImportCheck extends Check {
return root.filterCallback(function(token:TokenTree, depth:Int):FilterResult {
return switch (token.tok) {
case Kwd(KwdImport):
if (TokenTreeCheckUtils.isMetadata(token)) {
return SkipSubtree;
}
FoundGoDeeper;
default:
GoDeeper;
Expand Down Expand Up @@ -116,6 +119,7 @@ class UnusedImportCheck extends Check {
var moduleName:StringBuf = new StringBuf();

while (true) {
if (token == null) return moduleName.toString();
switch (token.tok) {
case Binop(OpMult):
return null;
Expand All @@ -140,6 +144,7 @@ class UnusedImportCheck extends Check {
function detectTypeName(token:TokenTree):String {
var lastName:String = null;
while (true) {
if (token == null) return lastName;
switch (token.tok) {
case Binop(OpMult):
return null;
Expand Down
8 changes: 8 additions & 0 deletions test/checkstyle/checks/imports/UnusedImportCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UnusedImportCheckTest extends CheckTestCase<UnusedImportCheckTests> {
assertNoMsg(check, IMPORT_BASE_CLASS);
assertNoMsg(check, IMPORT_AS);
assertNoMsg(check, IMPORT_IN_STATIC_FUNC);
assertNoMsg(check, METADATA_IMPORT);
}

@Test
Expand Down Expand Up @@ -258,4 +259,11 @@ enum abstract UnusedImportCheckTests(String) to String {
trace ('${Check3}');
}
}";
var METADATA_IMPORT = "
abstractAndClass Test {
private static var SRC =
{
@:import h3d.shader.BaseMesh;
};
}";
}

0 comments on commit 773bf04

Please sign in to comment.