Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detector for unused imports #1592

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions slither/detectors/all_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from .statements.boolean_constant_misuse import BooleanConstantMisuse
from .statements.divide_before_multiply import DivideBeforeMultiply
from .statements.unprotected_upgradeable import UnprotectedUpgradeable
from .statements.unused_imports import UnusedImports
from .slither.name_reused import NameReused

from .functions.unimplemented import UnimplementedFunctionDetection
Expand Down
526 changes: 526 additions & 0 deletions slither/detectors/statements/unused_imports.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Unused imports found in /home/bart/Documents/TOB/slither/tests/e2e/detectors/test_data/unused-imports/0.8.16/B.sol.
Consider removing the following imports:
tests/e2e/detectors/test_data/unused-imports/0.8.16/A.sol


Unused imports found in /home/bart/Documents/TOB/slither/tests/e2e/detectors/test_data/unused-imports/0.8.16/C.sol.
Consider removing the following imports:
tests/e2e/detectors/test_data/unused-imports/0.8.16/B.sol
and adding the following:
tests/e2e/detectors/test_data/unused-imports/0.8.16/A.sol


10 changes: 10 additions & 0 deletions tests/e2e/detectors/test_data/unused-imports/0.8.16/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

library A
{
function a() public
{

}
}
9 changes: 9 additions & 0 deletions tests/e2e/detectors/test_data/unused-imports/0.8.16/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./A.sol";

contract B
{

}
12 changes: 12 additions & 0 deletions tests/e2e/detectors/test_data/unused-imports/0.8.16/C.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./B.sol";

contract C
{
constructor()
{
A.a();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

library ConstantContractLevel
{
uint constant public CONSTANT = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./ConstantContractLevel.sol";

contract ConstantContractLevelUsedInContractTest
{
uint private v = ConstantContractLevel.CONSTANT;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./ConstantContractLevel.sol";

uint constant __ = ConstantContractLevel.CONSTANT;

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

uint constant ConstantTopLevel = 0;

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./ConstantTopLevel.sol";

contract ConstantTopLevelUsedInContractTest
{
uint private v = ConstantTopLevel;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./ConstantTopLevel.sol";

uint constant __ = ConstantTopLevel;

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy_
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

contract Contract
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./Contract.sol";

contract ContractUsedInContractTest1
{
Contract c;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./Contract.sol";

contract ContractUsedInContractTest2 is Contract
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./Contract.sol";

Contract constant c = Contract(address(0x0));

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

error err();

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomErrorTopLevel.sol";

contract CustomErrorTopLevelUsedInContractTest
{
constructor()
{
f();
}

function f() private pure
{
revert err();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

library CustomEventContractLevel
{
event CustomEvent();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomEventContractLevel.sol";

contract CustomEventContractLevelUsedInContractTest
{
function f() public
{
emit CustomEventContractLevel.CustomEvent();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomEventContractLevel.sol";

function f()
{
emit CustomEventContractLevel.CustomEvent();
}

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

contract CustomTypeContractLevel
{
type CustomType is uint;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeContractLevel.sol";

contract CustomTypeContractLevelUsedInContractTest1
{
CustomTypeContractLevel.CustomType private v;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeContractLevel.sol";

contract CustomTypeContractLevelUsedInContractTest2
{
function f(CustomTypeContractLevel.CustomType) public
{

}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeContractLevel.sol";

contract CustomTypeContractLevelUsedInContractTest3
{
modifier m()
{
CustomTypeContractLevel.CustomType ___;
_;
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeContractLevel.sol";

contract CustomTypeContractLevelUsedInContractTest4
{
struct CustomStruct
{
CustomTypeContractLevel.CustomType ___;
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeContractLevel.sol";

struct CustomTypeContractLevelUsedTopLevelTest1
{
CustomTypeContractLevel.CustomType __;
}

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeContractLevel.sol";

CustomTypeContractLevel.CustomType constant __ = CustomTypeContractLevel.CustomType.wrap(0);

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

type CustomType is uint;

// dummy contract, so that "No contract were found ..." message is not being thrown by Slither
contract Dummy
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeTopLevel.sol";

contract CustomTypeTopLevelUsedInContractTest1
{
CustomType private v;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeTopLevel.sol";

contract CustomTypeTopLevelUsedInContractTest2
{
function f(CustomType) public
{

}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.16;

import "./CustomTypeTopLevel.sol";

contract CustomTypeTopLevelUsedInContractTest3
{
modifier m()
{
CustomType v;
_;
}
}
Binary file not shown.
Loading