From f40edeb717239f33b3637b2d98b233664c5b68fa Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 1 Sep 2020 16:38:32 -0700 Subject: [PATCH 1/2] Add description and example to C26460 --- docs/code-quality/c26460.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/code-quality/c26460.md b/docs/code-quality/c26460.md index 26040c71375..2ca66ded4aa 100644 --- a/docs/code-quality/c26460.md +++ b/docs/code-quality/c26460.md @@ -4,7 +4,34 @@ ms.date: 03/22/2018 ms.topic: reference f1_keywords: ["C26460"] helpviewer_keywords: ["C26460"] +description: CppCoreCheck rule that enforces C++ Core Guidelines Con.3 --- # C26460 USE_CONST_REFERENCE_ARGUMENTS +The reference argument '%argument%' for function '%function%' can be marked as `const` (con.3). - The reference argument '%argument%' for function '%function%' can be marked as `const`. See [C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref). +Passing an object by reference indicates that the function has the potential modify the object. If that is not the intent of the function, it is better to mark the argument as a const reference. + +## See also +[C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref). + +## Example +```cpp +struct MyStruct +{ + void MemberFn1() const; + void MemberFn2(); +}; + + +void Function1_Helper(const MyStruct&); +void Function1(MyStruct& myStruct) // C26460, see comments below. +{ + myStruct.MemberFn1(); // The member function is marked as const + Function1_Helper(myStruct); // Function1_Helper takes a const reference +} + +void Function2(MyStruct& myStruct) +{ + myStruct.MemberFn2(); // MemberFn2 is non-const and has the potential to modify data +} +``` From 1fa81733e7353729e7744a96a56051ea38cc5090 Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 1 Sep 2020 17:25:04 -0700 Subject: [PATCH 2/2] Add description and example to C26461 --- docs/code-quality/c26461.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/code-quality/c26461.md b/docs/code-quality/c26461.md index f5a2818428e..5cd6d719b79 100644 --- a/docs/code-quality/c26461.md +++ b/docs/code-quality/c26461.md @@ -4,7 +4,40 @@ ms.date: 03/22/2018 ms.topic: reference f1_keywords: ["C26461"] helpviewer_keywords: ["C26461"] +description: CppCoreCheck rule that enforces C++ Core Guidelines con.3 --- # C26461 USE_CONST_POINTER_ARGUMENTS: -The pointer argument '%argument%' for function '%function%' can be marked as a pointer to `const`. See [C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref). +The pointer argument '%argument%' for function '%function%' can be marked as a pointer to `const` (con.3). + +A function with a `T*` argument has the potential to modify the value of the object. If that is not the intent of the function, it is better to make the pointer a `const T*` instead. + +## See also +[C++ Core Guidelines con.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rconst-ref). + +## Example +```cpp +struct MyStruct +{ + void MemberFn1() const; + void MemberFn2(); +}; + +void Function1_Helper(const MyStruct* myStruct); +void Function1(MyStruct* myStruct) // C26461, neither of the operations on myStruct would modify the pointer's value. +{ + if (!myStruct) + return; + + myStruct->MemberFn1(); // The member function is const + Function1_Helper(myStruct); // Function1_Helper takes a const +} + +void Function2(MyStruct* myStruct) +{ + if (!myStruct) + return; + + myStruct->MemberFn2(); // The member function is non-const, so no C26461 will be issued +} +```