diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2160,7 +2160,8 @@ // Deduplicate #includes. Indices.erase(std::unique(Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) { - return Includes[LHSI].Text == Includes[RHSI].Text; + return Includes[LHSI].Text.trim() == + Includes[RHSI].Text.trim(); }), Indices.end()); diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -266,12 +266,19 @@ sort(" #include \"a.h\"\n" " #include \"c.h\"\n" " #include \"b.h\"\n")); + EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n" + " #include \"a.h\"\n")); +} + +TEST_F(SortIncludesTest, TrailingWhitespace) { EXPECT_EQ("#include \"a.h\"\n" "#include \"b.h\"\n" "#include \"c.h\"\n", - sort("# include \"a.h\"\n" - "# include \"c.h\"\n" - "# include \"b.h\"\n")); + sort("#include \"a.h\" \n" + "#include \"c.h\" \n" + "#include \"b.h\" \n")); + EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n" + "#include \"a.h\" \n")); } TEST_F(SortIncludesTest, GreaterInComment) {