vscode-solidity-inspector icon indicating copy to clipboard operation
vscode-solidity-inspector copied to clipboard

Unused import false positive when using unnamed imports

Open kopy-kat opened this issue 2 years ago • 4 comments
trafficstars

Sometimes, the extension will highlight an unnamed import (eg `import "../src/Contract.sol") as unused even though it is used. In my case, this happens when the file importing from has multiple contracts but I don't know whether this is actually the issue. If this is useful, I can experiment and try to find the root cause of the issue

kopy-kat avatar Nov 14 '23 08:11 kopy-kat

Thanks for reporting! Will look into it soon.

PraneshASP avatar Nov 28 '23 08:11 PraneshASP

Sorry for the delayed response.

The issue arises if you have use an imported contract from other file.

For ex, you have 2 contracts: Contract A and Contract B. If Contract B tries to use imports from contract A, it will not be detected by this extension. For now, it only detects unused imports in the current file to keep things simple. I agree it would be more useful to detect unused imports across different files.

If you can add the feature, PRs are more than welcome :) I'm happy to review!

PraneshASP avatar Jan 17 '24 14:01 PraneshASP

Another false positive is happening with free functions, if you don't use named imports the extension will mark the whole import as unused.

clemlak avatar Feb 13 '24 14:02 clemlak

Agreed. Currently, highlighting unused imports works best for named imports.

I believe named imports should be adopted by default so that readers know what contracts are being used from the imported file. It also helps to make the imports consistent. Unfortunately that's not the case now.

One way to fix this is to index the contract names in each of the files and check if the contract has been used in the current file. I wanted to keep it simple, but this change looks quite complex and I also suspect it might have some perf issues for larger projects. Other option is to ignore unused import highlights for unnamed imports.

PraneshASP avatar Feb 13 '24 18:02 PraneshASP