Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lint/unused_import_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,64 @@ var UnusedImportAnalyzer = (func() *analysis.Analyzer {
}
},
)
// TODO: Remove the below section once https://github.com/onflow/cadence/issues/4470 is fixed.
// The Walk methods for composite, interface, and attachment declarations
// do not recurse into conformance lists, attachment base types, or
// entitlement access specifiers, so the inspector never visits the
// NominalType nodes there. Check those positions explicitly.

markNominalTypeUsed := func(t *ast.NominalType) {
name := t.Identifier.Identifier
if _, isImported := importedNames[name]; isImported {
usedImports[name] = struct{}{}
}
}

markEntitlementAccessUsed := func(access ast.Access) {
entAccess, ok := access.(ast.EntitlementAccess)
if !ok {
return
}
for _, e := range entAccess.EntitlementSet.Entitlements() {
markNominalTypeUsed(e)
}
}

inspector.Preorder(
[]ast.Element{
(*ast.CompositeDeclaration)(nil),
(*ast.InterfaceDeclaration)(nil),
(*ast.AttachmentDeclaration)(nil),
(*ast.FunctionDeclaration)(nil),
(*ast.FieldDeclaration)(nil),
},
func(element ast.Element) {
switch decl := element.(type) {
case *ast.CompositeDeclaration:
markEntitlementAccessUsed(decl.Access)
for _, c := range decl.Conformances {
markNominalTypeUsed(c)
}
case *ast.InterfaceDeclaration:
markEntitlementAccessUsed(decl.Access)
for _, c := range decl.Conformances {
markNominalTypeUsed(c)
}
case *ast.AttachmentDeclaration:
markEntitlementAccessUsed(decl.Access)
if decl.BaseType != nil {
markNominalTypeUsed(decl.BaseType)
}
for _, c := range decl.Conformances {
markNominalTypeUsed(c)
}
case *ast.FunctionDeclaration:
markEntitlementAccessUsed(decl.Access)
case *ast.FieldDeclaration:
markEntitlementAccessUsed(decl.Access)
}
},
)

// Collect unused imports.
// For implicit imports, we only report if ALL imports from that declaration are unused
Expand Down
96 changes: 96 additions & 0 deletions lint/unused_import_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,96 @@ func TestUnusedImportAnalyzer(t *testing.T) {
)
})

t.Run("used only in composite conformance list", func(t *testing.T) {
t.Parallel()

const code = `
import Bar from 0x01

access(all) resource Foo: Bar.Interface {}
`

diagnostics := testAnalyzersWithImports(t, code, lint.UnusedImportAnalyzer)
require.Equal(t,
[]analysis.Diagnostic(nil),
diagnostics,
)
})

t.Run("used only in interface conformance list", func(t *testing.T) {
t.Parallel()

const code = `
import Bar from 0x01

access(all) resource interface MyInterface: Bar.Interface {}
`

diagnostics := testAnalyzersWithImports(t, code, lint.UnusedImportAnalyzer)
require.Equal(t,
[]analysis.Diagnostic(nil),
diagnostics,
)
})

t.Run("used only in attachment base type", func(t *testing.T) {
t.Parallel()

const code = `
import Bar from 0x01

access(all) attachment MyAttachment for Bar.Resource {}
`

diagnostics := testAnalyzersWithImports(t, code, lint.UnusedImportAnalyzer)
require.Equal(t,
[]analysis.Diagnostic(nil),
diagnostics,
)
})

t.Run("used only in entitlement access on function member", func(t *testing.T) {
t.Parallel()

const code = `
import Bar from 0x01

access(all) resource Foo {
access(Bar.Entitlement) fun method(): Int {
return 1
}
}
`

diagnostics := testAnalyzersWithImports(t, code, lint.UnusedImportAnalyzer)
require.Equal(t,
[]analysis.Diagnostic(nil),
diagnostics,
)
})

t.Run("used only in entitlement access on field member", func(t *testing.T) {
t.Parallel()

const code = `
import Bar from 0x01

access(all) resource Foo {
access(Bar.Entitlement) let value: Int

init() {
self.value = 0
}
}
`

diagnostics := testAnalyzersWithImports(t, code, lint.UnusedImportAnalyzer)
require.Equal(t,
[]analysis.Diagnostic(nil),
diagnostics,
)
})

t.Run("no imports", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -559,6 +649,12 @@ func testAnalyzersWithImports(t *testing.T, code string, analyzers ...*analysis.

barContract := `
access(all) contract Bar {
access(all) entitlement Entitlement

access(all) resource interface Interface {}

access(all) resource Resource {}

access(all) struct SomeStruct {}

access(all) fun doSomething() {}
Expand Down
Loading