| #!/bin/bash |
| |
| # Detect trivial unused code. |
| # |
| # Automatically removal is possible, but is considered an unsafe operation. When a |
| # change hasn't been commited, automatic removal could cause unintended irreversible |
| # loss of in-progress code. |
| # |
| # Note: This cannot detect unused code between modules or packages. For complex unused |
| # code detection, vulture should be used. |
| autoflake \ |
| --quiet \ |
| --check-diff \ |
| --remove-duplicate-keys \ |
| --remove-unused-variables \ |
| --remove-all-unused-imports \ |
| --recursive . |
| |
| if [ $? -eq 0 ]; then |
| echo "No unused code found" |
| else |
| echo "" |
| echo "=====================" |
| echo "Unused code detected!" |
| echo "=====================" |
| echo "" |
| echo "If these changes are trivial, consider running:" |
| echo "\"autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r .\"" |
| echo "" |
| read -p "Run this command to remove all unused code? [y/n] " -n 1 -r |
| echo "" |
| echo "" |
| |
| if [[ $REPLY =~ ^[Yy]$ ]]; then |
| autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r . |
| else |
| exit 1 |
| fi |
| fi |
| |
| # Sort imports to avoid bikeshedding. |
| isort . |
| |
| # Format code; also to avoid bikeshedding. |
| black . |
| |