diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 0dee95d0..40d0bfb7 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -347,6 +347,8 @@ phutil_register_library_map(array( 'ArcanistUnnecessaryFinalModifierXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnnecessaryFinalModifierXHPASTLinterRule.php', 'ArcanistUnnecessaryFinalModifierXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnnecessaryFinalModifierXHPASTLinterRuleTestCase.php', 'ArcanistUnnecessarySemicolonXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnnecessarySemicolonXHPASTLinterRule.php', + 'ArcanistUnnecessarySymbolAliasXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php', + 'ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php', 'ArcanistUnsafeDynamicStringXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnsafeDynamicStringXHPASTLinterRule.php', 'ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase.php', 'ArcanistUpgradeWorkflow' => 'workflow/ArcanistUpgradeWorkflow.php', @@ -727,6 +729,8 @@ phutil_register_library_map(array( 'ArcanistUnnecessaryFinalModifierXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUnnecessaryFinalModifierXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistUnnecessarySemicolonXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', + 'ArcanistUnnecessarySymbolAliasXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', + 'ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistUnsafeDynamicStringXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistUpgradeWorkflow' => 'ArcanistWorkflow', diff --git a/src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php new file mode 100644 index 00000000..f2b1fb09 --- /dev/null +++ b/src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php @@ -0,0 +1,43 @@ +selectDescendantsOfType('n_USE'); + + foreach ($uses as $use) { + $symbol = $use->getChildOfType(0, 'n_SYMBOL_NAME'); + $alias = $use->getChildByIndex(1); + + if ($alias->getTypeName() == 'n_EMPTY') { + continue; + } + + $symbol_name = last(explode('\\', $symbol->getConcreteString())); + $alias_name = $alias->getConcreteString(); + + if ($symbol_name == $alias_name) { + $this->raiseLintAtNode( + $use, + pht( + 'Importing `%s` with `%s` is unnecessary because the aliased '. + 'name is identical to the imported symbol name.', + $symbol->getConcreteString(), + sprintf('as %s', $alias_name)), + $symbol->getConcreteString()); + } + } + } + +} diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php new file mode 100644 index 00000000..21ce2687 --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php @@ -0,0 +1,11 @@ +executeTestsInDirectory( + dirname(__FILE__).'/unnecessary-symbol-alias/'); + } + +} diff --git a/src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test b/src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test new file mode 100644 index 00000000..a9a25d5c --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test @@ -0,0 +1,16 @@ +