Skip to content

Commit a6ce08a

Browse files
jmrcopybara-github
authored andcommitted
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
1 parent c00fd25 commit a6ce08a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

docs/reference/matchers.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ messages, you can use:
171171
| `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`. |
172172
| `Property(property_name, &class::property, m)` | The same as the two-parameter version, but provides a better error message.
173173

174+
{: .callout .warning}
175+
Warning: Don't use `Property()` against member functions that you do not own,
176+
because taking addresses of functions is fragile and generally not part of the
177+
contract of the function.
178+
174179
**Notes:**
175180

176181
* You can use `FieldsAre()` to match any type that supports structured
@@ -189,10 +194,6 @@ messages, you can use:
189194
EXPECT_THAT(s, FieldsAre(42, "aloha"));
190195
```
191196

192-
* Don't use `Property()` against member functions that you do not own, because
193-
taking addresses of functions is fragile and generally not part of the
194-
contract of the function.
195-
196197
## Matching the Result of a Function, Functor, or Callback
197198

198199
| Matcher | Description |

googlemock/include/gmock/gmock-matchers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,6 +4453,10 @@ inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field(
44534453
// matches 'matcher'. For example,
44544454
// Property(&Foo::str, StartsWith("hi"))
44554455
// matches a Foo object x if and only if x.str() starts with "hi".
4456+
//
4457+
// Warning: Don't use `Property()` against member functions that you do not
4458+
// own, because taking addresses of functions is fragile and generally not part
4459+
// of the contract of the function.
44564460
template <typename Class, typename PropertyType, typename PropertyMatcher>
44574461
inline PolymorphicMatcher<internal::PropertyMatcher<
44584462
Class, PropertyType, PropertyType (Class::*)() const>>

0 commit comments

Comments
 (0)