From a6ce08abf746c0aaa577520d6d1f6ea2abeeb61d Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock <jmr@google.com> Date: Mon, 17 Feb 2025 00:59:00 -0800 Subject: [PATCH] Property: Promote note to warning Property() should not be used against member functions that you do not own. Make this warning more prominent in matchers.md and add it to gmock-matchers.h. PiperOrigin-RevId: 727753777 Change-Id: Idf9155103b04b98efc957ff0d0cf3a8d8662eb72 --- docs/reference/matchers.md | 9 +++++---- googlemock/include/gmock/gmock-matchers.h | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/reference/matchers.md b/docs/reference/matchers.md index 243e3f95..16397ef2 100644 --- a/docs/reference/matchers.md +++ b/docs/reference/matchers.md @@ -171,6 +171,11 @@ messages, you can use: | `Property(&class::property, m)` | `argument.property()` (or `argument->property()` when `argument` is a plain pointer) matches matcher `m`, where `argument` is an object of type _class_. The method `property()` must take no argument and be declared as `const`. | | `Property(property_name, &class::property, m)` | The same as the two-parameter version, but provides a better error message. +{: .callout .warning} +Warning: Don't use `Property()` against member functions that you do not own, +because taking addresses of functions is fragile and generally not part of the +contract of the function. + **Notes:** * You can use `FieldsAre()` to match any type that supports structured @@ -189,10 +194,6 @@ messages, you can use: EXPECT_THAT(s, FieldsAre(42, "aloha")); ``` -* Don't use `Property()` against member functions that you do not own, because - taking addresses of functions is fragile and generally not part of the - contract of the function. - ## Matching the Result of a Function, Functor, or Callback | Matcher | Description | diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index e979544c..e5ee543f 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -4453,6 +4453,10 @@ inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field( // matches 'matcher'. For example, // Property(&Foo::str, StartsWith("hi")) // matches a Foo object x if and only if x.str() starts with "hi". +// +// Warning: Don't use `Property()` against member functions that you do not +// own, because taking addresses of functions is fragile and generally not part +// of the contract of the function. template <typename Class, typename PropertyType, typename PropertyMatcher> inline PolymorphicMatcher<internal::PropertyMatcher< Class, PropertyType, PropertyType (Class::*)() const>>