Lint: Redundant imports
Following the Java language, redundant imports are allowed and just ignored.
But now the linter detects and complains about it as violating "unique-import".
Bug: 215566892
Test: aidl_unittests
Change-Id: Ibe575fa73f8e47c8646f1807f7c9df94f2a9ce7a
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 50b0739..2d8886b 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -1738,7 +1738,7 @@
}
AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
- std::set<string> imports,
+ std::vector<string> imports,
std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
bool is_preprocessed)
: AidlCommentable(location, comments),
diff --git a/aidl_language.h b/aidl_language.h
index d19e425..0f4e2d1 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -1202,7 +1202,7 @@
class AidlDocument : public AidlCommentable, public AidlScope {
public:
AidlDocument(const AidlLocation& location, const Comments& comments,
- std::set<std::string> imports,
+ std::vector<std::string> imports,
std::vector<std::unique_ptr<AidlDefinedType>> defined_types, bool is_preprocessed);
~AidlDocument() = default;
@@ -1213,7 +1213,7 @@
AidlDocument& operator=(AidlDocument&&) = delete;
std::string ResolveName(const std::string& name) const override;
- const std::set<std::string>& Imports() const { return imports_; }
+ const std::vector<std::string>& Imports() const { return imports_; }
const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
return defined_types_;
}
@@ -1227,7 +1227,7 @@
void DispatchVisit(AidlVisitor& v) const override { v.Visit(*this); }
private:
- const std::set<std::string> imports_;
+ const std::vector<std::string> imports_;
const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
bool is_preprocessed_;
};
diff --git a/aidl_language_y.yy b/aidl_language_y.yy
index 1665709..7f0b5bf 100644
--- a/aidl_language_y.yy
+++ b/aidl_language_y.yy
@@ -188,10 +188,9 @@
} else if (!$2->empty()) {
comments = $2->front()->GetComments();
}
- // dedup imports
- std::set<std::string> imports;
+ std::vector<std::string> imports;
for (const auto& import : *$2) {
- imports.insert(import->GetText());
+ imports.push_back(import->GetText());
}
ps->MakeDocument(loc(@1), comments, std::move(imports), std::move(*$3));
delete $1;
diff --git a/diagnostics_unittest.cpp b/diagnostics_unittest.cpp
index 8c13df4..0066b9f 100644
--- a/diagnostics_unittest.cpp
+++ b/diagnostics_unittest.cpp
@@ -218,12 +218,12 @@
"interface IFoo{}"}});
}
-TEST_F(DiagnosticsTest, AllowRedundantImports) {
- expect_diagnostics = {};
+TEST_F(DiagnosticsTest, RedundantImports) {
+ expect_diagnostics = {DiagnosticID::unique_import};
ParseFiles({{"p/IFoo.aidl",
"package p;\n"
"import q.IBar;\n"
- "import q.IBar;\n" // ugly, but okay
+ "import q.IBar;\n"
"interface IFoo{}"},
{"q/IBar.aidl", "package q; interface IBar{}"}});
}
diff --git a/parser.cpp b/parser.cpp
index 0c2677a..9988f56 100644
--- a/parser.cpp
+++ b/parser.cpp
@@ -310,7 +310,7 @@
}
void Parser::MakeDocument(const AidlLocation& location, const Comments& comments,
- std::set<std::string> imports,
+ std::vector<std::string> imports,
std::vector<std::unique_ptr<AidlDefinedType>> defined_types) {
AIDL_FATAL_IF(document_.get(), location);
document_ = std::make_unique<AidlDocument>(location, comments, std::move(imports),
diff --git a/parser.h b/parser.h
index e497912..074d6d5 100644
--- a/parser.h
+++ b/parser.h
@@ -92,7 +92,7 @@
const std::string& Package() const { return package_; }
void MakeDocument(const AidlLocation& location, const Comments& comments,
- std::set<std::string> imports,
+ std::vector<std::string> imports,
std::vector<std::unique_ptr<AidlDefinedType>> defined_types);
private: