Extend presubmit _CheckUniquePtr to multiline
The presubmit check _CheckUniquePtr guards against calling
std::unique_ptr constructor directly, instead directing code authors to
use std::make_unique.
This check currently fails to match multiline expressions. While it
catches
bar = std::unique_ptr<T>(foo);
it does not catch
bar =
std::unique_ptr<T>(foo);
nor does it catch
bar = std::unique_ptr<T>(
foo);
This CL fixes it by extending the match pattern to catch all lines with
the substring "std::unique_ptr<T>(". (But not those with
"std::unique_ptr<T>()", which should be handled by the "nullptr-check".)
Bug: 827961
Change-Id: I376b5e9811418205e294e97de0b6b7bcbf6891d2
Reviewed-on: https://chromium-review.googlesource.com/989735
Commit-Queue: Vaclav Brozek <[email protected]>
Reviewed-by: Jochen Eisinger <[email protected]>
Cr-Commit-Position: refs/heads/master@{#548045}
diff --git a/PRESUBMIT_test.py b/PRESUBMIT_test.py
index a07ba05..f424d401 100755
--- a/PRESUBMIT_test.py
+++ b/PRESUBMIT_test.py
@@ -1646,21 +1646,31 @@
MockFile('dir/java/src/bar.mm', ['bar = std::unique_ptr<T>(foo)']),
MockFile('dir/java/src/baz.cc', ['std::unique_ptr<T>()']),
MockFile('dir/java/src/baz-p.cc', ['std::unique_ptr<T<P>>()']),
- # TODO(crbug.com/827961) Fix multi-line support.
- #MockFile('dir/java/src/mult.cc', [
- # 'barVeryVeryLongLongBaaaaaarSoThatTheLineLimitIsAlmostReached =',
- # ' std::unique_ptr<T>(foo);'
- #]),
+ MockFile('dir/java/src/mult.cc', [
+ 'return',
+ ' std::unique_ptr<T>(barVeryVeryLongFooSoThatItWouldNotFitAbove);'
+ ]),
+ MockFile('dir/java/src/mult2.cc', [
+ 'barVeryVeryLongLongBaaaaaarSoThatTheLineLimitIsAlmostReached =',
+ ' std::unique_ptr<T>(foo);'
+ ]),
+ MockFile('dir/java/src/mult3.cc', [
+ 'bar = std::unique_ptr<T>(',
+ ' fooVeryVeryVeryLongStillGoingWellThisWillTakeAWhileFinallyThere);'
+ ]),
]
results = PRESUBMIT._CheckUniquePtr(mock_input_api, MockOutputApi())
# TODO(crbug.com/827961) Make the check return just one result, listing all
# affected files in it.
- self.assertEqual(4, len(results))
+ self.assertEqual(7, len(results))
self.assertTrue('foo.cc' in results[0].message)
self.assertTrue('bar.mm' in results[1].message)
self.assertTrue('baz.cc' in results[2].message)
self.assertTrue('baz-p.cc' in results[3].message)
+ self.assertTrue('mult.cc' in results[4].message)
+ self.assertTrue('mult2.cc' in results[5].message)
+ self.assertTrue('mult3.cc' in results[6].message)
def testFalsePositives(self):
mock_input_api = MockInputApi()