{"id":26461,"date":"2020-09-04T15:00:42","date_gmt":"2020-09-04T15:00:42","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=26461"},"modified":"2020-09-08T11:22:03","modified_gmt":"2020-09-08T11:22:03","slug":"new-safety-rules-in-c-core-check","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/new-safety-rules-in-c-core-check\/","title":{"rendered":"New safety rules in C++ Core Check"},"content":{"rendered":"<p><span data-contrast=\"auto\">Rust and C++ are two popular systems programming languages.\u00a0<\/span><span data-contrast=\"auto\">For years, the focus of C++ has been on performance.\u00a0<\/span><span data-contrast=\"auto\">W<\/span><span data-contrast=\"auto\">e are increasingly hearing<\/span><span data-contrast=\"auto\"> calls from customers and security researchers that C++ should have stronger safety guarantees in the language.<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">C++ often falls behind Rust when it comes to programming safety.\u00a0<\/span><a href=\"https:\/\/visualstudio.microsoft.com\/vs\/\"><span data-contrast=\"none\">Visual Studio 2019 version 16.7<\/span><\/a><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">contains\u00a0<\/span><span data-contrast=\"auto\">four new rules\u00a0<\/span><span data-contrast=\"auto\">in C++ Core Check\u00a0<\/span><span data-contrast=\"auto\">to incorporate some safety features from Rust into C++<\/span><span data-contrast=\"auto\">.<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For more detailed information<\/span><span data-contrast=\"auto\">\u00a0on C++ Core Check<\/span><span data-contrast=\"auto\">, please see the\u202f<\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/code-quality\/code-analysis-for-cpp-corecheck\"><span data-contrast=\"none\">C++ Core Guidelines Checker Reference\u202f<\/span><\/a><span data-contrast=\"auto\">documentation.<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">If you\u2019re just getting started with native code analysis tools, take a look at our introductory\u202f<\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/code-quality\/quick-start-code-analysis-for-c-cpp\"><span data-contrast=\"none\">quick start for Code Analysis for C\/C++<\/span><\/a><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Missin<\/span><span data-contrast=\"none\">g <code>default <\/code><\/span><span data-contrast=\"none\">label in switch statements<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">Rust&#8217;s pattern matching constructs can be used similarly to the C++ <code>switch<\/code> statement. One way in which they differ, however, is that the Rust equivalent requires the programmer to cover all possible p<\/span><span data-contrast=\"auto\">atterns being matched. This can be achieved either through writing an explicit handler for each\u00a0<\/span><span data-contrast=\"auto\">pattern or<\/span><span data-contrast=\"auto\">\u00a0appending a default handler for cases not explicitly covered.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For example, the following Rust code would not compile if the default handler\u00a0<\/span><span data-contrast=\"auto\">were<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">missin<\/span><span data-contrast=\"auto\">g.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">\/\/ i32 == 32-bit signed integer\u00a0\r\nfn printDiceRoll(roll: i32) {\u00a0\r\n\u00a0\u00a0\u00a0 match roll {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 =&gt; println!(\"one!\"),\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2 =&gt; println!(\"two!\"),\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3 =&gt; println!(\"three!\"),\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4 =&gt; println!(\"four!\"),\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5 =&gt; println!(\"five!\"),\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 6 =&gt; println!(\"six!\"),\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _ =&gt; println!(\"what kind of dice are you using?\") \/\/ default handler\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">This is a neat little safety feature because it guards against this\u00a0<\/span><span data-contrast=\"auto\">extremely easy<\/span><span data-contrast=\"auto\">\u00a0to make, but not so easy to catch, progr<\/span><span data-contrast=\"auto\">amming error.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Visual Studio does warn whenever all cases of an <\/span><code><span data-contrast=\"auto\">enum<\/span><\/code><span data-contrast=\"auto\">\u00a0type are not covered in a C++ switch statement &lt;C4061, C4062&gt;. However, such a warning is not present for other types, such as integers, as in the Rust example above.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">This release\u00a0<\/span><span data-contrast=\"auto\">introduces a new check to warn whenever switch statements over non-<\/span><span data-contrast=\"auto\">enum<\/span><span data-contrast=\"auto\"> types (i.e., <code>char<\/code>, <code>int<\/code>, &#8230;) are missing a <code>default<\/code> label. <\/span><span data-contrast=\"auto\">You can find detailed documentation on this check <\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/code-quality\/c26818?view=vs-2019\"><span data-contrast=\"auto\">here<\/span><\/a><span data-contrast=\"auto\">.\u00a0<\/span><span data-contrast=\"auto\">To enable this rule in Visual Studio, you will have to select the ruleset \u201cC++ Core Check Style Rules\u201d, \u201cC++ Core Check Rules\u201d, or \u201cMicrosoft All Rules\u201d for your project and then run code analysis.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Re-writin<\/span><span data-contrast=\"auto\">g the Rust example from above in C++,\u00a0<\/span><span data-contrast=\"auto\">we would<\/span><span data-contrast=\"auto\">\u00a0get something like<\/span><span data-contrast=\"auto\">\u00a0below.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">void printDiceRoll(int roll) {\u00a0\r\n\u00a0\u00a0\u00a0 switch (roll) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"one\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 2:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"two\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 3:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"three\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 4:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"four\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 5:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"five\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 6:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"six\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"what kind of dice are you using?\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">Removing t<\/span><span data-contrast=\"auto\">he <code>default<\/code> handler now results in a warning<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">void printDiceRoll(int roll) {\u00a0\r\n\u00a0\u00a0\u00a0 switch (roll) { \/\/ warning C26818: Switch statement does not cover all cases. Consider adding a 'default' label (es.79)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"one\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 2:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"two\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 3:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"three\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 4:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"four\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 5:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"five\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 6:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::cout &lt;&lt; \"six\\n\";\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Unannotated\u00a0<\/span><span data-contrast=\"none\">fallthrough<\/span><span data-contrast=\"none\">\u00a0in swit<\/span><span data-contrast=\"none\">ch statements<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">Another restriction of Rust&#8217;s <code>match<\/code>\u00a0statement is that it does not support the notion of <\/span><code><span data-contrast=\"auto\">fallthrough<\/span><\/code><span data-contrast=\"auto\">\u00a0between cases. In C++, on the other hand, the following code is perfectly valid<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">enum\u00a0class Food {\u00a0\r\n\u00a0\u00a0\u00a0 BANANA, ORANGE, PIZZA, CAKE, KALE, CELERY\u00a0\r\n};\u00a0\r\n\r\nvoid\u00a0takeFromFridge(Food food) {\u00a0\r\n\u00a0\u00a0\u00a0 switch (food) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::BANANA:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::ORANGE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 peel(food);\u00a0\u00a0\u00a0\/\/ implicit\u00a0fallthrough\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::PIZZA:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::CAKE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0eat(food);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::KALE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::CELERY:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throwOut(food);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">While this example is perfectly sound, implicit\u00a0<\/span><span data-contrast=\"auto\">fallthrough<\/span><span data-contrast=\"auto\">\u00a0between cases can very easily be a b<\/span><span data-contrast=\"auto\">ug. Say, for instance, that the programmer of the above function had forgotten the <code>break<\/code> statement after the call to <code>eat(food)<\/code>. The code would run, but the behavior would be completely incorrect. With a larger and more complex codebase, tracking this t<\/span><span data-contrast=\"auto\">ype of bug can be\u00a0<\/span><span data-contrast=\"auto\">difficult<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Fortunately, with C++17 comes the addition of the <span style=\"color: #222222; font-family: Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace;\"><span style=\"font-size: 14.4px; background-color: #e9ebec;\">[[fallthrough]] <\/span><\/span><\/span><span data-contrast=\"auto\">annotation, whose purpose is to mark <\/span><span data-contrast=\"auto\">fallthrough<\/span><span data-contrast=\"auto\">\u00a0between case labels, such as in the example above, so that maintainers of the code can be confident th<\/span><span data-contrast=\"auto\">e\u00a0<\/span><span data-contrast=\"auto\">fallthrough<\/span><span data-contrast=\"auto\">\u00a0behavior is intended.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">With <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/\">Visual Studio 2019 version 16.7<\/a>, warning C26819 <\/span><span data-contrast=\"auto\">is raised whenever a non-empty switch case falls through into a following case without marking the\u00a0<\/span><span data-contrast=\"auto\">fallthrough<\/span><span data-contrast=\"auto\"> using the <span style=\"color: #222222; font-family: Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace;\"><span style=\"font-size: 14.4px; background-color: #e9ebec;\">[[fallthrough]]<\/span><\/span><\/span><span data-contrast=\"auto\">\u00a0annotation. Detailed do<\/span><span data-contrast=\"auto\">cumentation can be found <\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/code-quality\/c26819?view=vs-2019\"><span data-contrast=\"auto\">here<\/span><\/a><span data-contrast=\"auto\">. <\/span><span data-contrast=\"auto\">This rule is enabled by default in Visual Studio<\/span><span data-contrast=\"auto\">\u00a0when you run code analysis.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">void\u00a0takeFromFridge(Food food) {\u00a0\r\n\u00a0\u00a0\u00a0 switch (food) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::BANANA: \/\/ empty case,\u00a0fallthrough\u00a0annotation not needed\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::ORANGE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 peel(food);\u00a0\u00a0\u00a0\u00a0\/\/ warning C26819: Unannotated\u00a0fallthrough\u00a0between switch labels (es.78)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::PIZZA:\u00a0 \/\/ empty case,\u00a0fallthrough\u00a0annotation not needed\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::CAKE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 eat(food);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::KALE:\u00a0 \/\/ empty case,\u00a0fallthrough\u00a0annotation not needed\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::CELERY:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throwOut(food);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">To fix this warning, insert a <code>[[fallthrough]]<\/code><\/span><span data-contrast=\"auto\">\u00a0statement<\/span><span data-contrast=\"auto\">.<\/span><\/p>\n<pre class=\"prettyprint\">void\u00a0takeFromFridge(Food food) {\u00a0\r\n\u00a0\u00a0\u00a0 switch (food) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::BANANA:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::ORANGE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 peel(food);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [[fallthrough]]; \/\/ the\u00a0fallthrough\u00a0is intended\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::PIZZA:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::CAKE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 eat(food);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::KALE:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case\u00a0Food::CELERY:\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throwOut(food);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Expensive range-for copy<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">A major difference between Rust and C++ is that Rust is m<\/span><span data-contrast=\"auto\">ove-by-default rather than copy-by-default.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p>Some Rust code:<\/p>\n<pre class=\"prettyprint\">struct MySequence {\r\n    sequence: Vec&lt;i32&gt;\r\n}\r\n\r\nfn main() {\r\n    let mut a = MySequence { sequence: vec![0, 1, 1, 2, 3, 5, 8, 13] };\r\n    let mut _b = a;\r\n    a.sequence.push(21); <span class=\"TextRun SCXW40379417 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW40379417 BCX8\" data-ccp-parastyle=\"Plain Text\">\/\/ error! `a` was move<\/span><\/span><span class=\"TextRun SCXW40379417 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW40379417 BCX8\" data-ccp-parastyle=\"Plain Text\">d into `b` and can no longer be used<\/span><\/span>\r\n}<\/pre>\n<p><span data-contrast=\"auto\">This means that explicit copy semantics must be used in most cases whenever a copy is intended.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">#[derive(Clone)]\r\nstruct MySequence {\r\n    sequence: Vec&lt;i32&gt;\r\n}\r\n\r\nfn main() {\r\n    let mut a = MySequence { sequence: vec![0, 1, 1, 2, 3, 5, 8, 13] };\r\n    let mut _b = a.clone();\r\n    a.sequence.push(21); \/\/ much better\r\n}<\/pre>\n<p><span data-contrast=\"auto\">C++, on the other hand, is copy-by-default. This is not a problem in\u00a0<\/span><span data-contrast=\"auto\">general but<\/span><span data-contrast=\"auto\">\u00a0can sometimes be a source of bugs. One case where th<\/span><span data-contrast=\"auto\">is commonly occurs is within range-for statements. Take for example the following piece of code<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">struct Person {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::string\u00a0first_name;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::string\u00a0last_name;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::string\u00a0email_address;\u00a0\r\n};\u00a0\r\n\r\nvoid\u00a0emailEveryoneInCompany(const std::vector&lt;Person&gt;&amp; employees) {\u00a0\r\n\u00a0\u00a0\u00a0 Email\u00a0email;\u00a0\r\n\u00a0\u00a0\u00a0 for (Person p: employees)\u00a0{ \/\/ copy of type `Person` occurs on each iteration\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0email.addRecipient(p.email_address);\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0email.addBody(\"Sorry for the spam\");\u00a0\r\n\u00a0\u00a0\u00a0\u00a0email.send();\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">In the above snippet, each element of the vector is copied into <code>p<\/code> on each iteration of the loop. This is not obvious and can be a significant source of inefficiency if the copy is expensive. To remedy this unnecessary copy, <\/span><span data-contrast=\"auto\">we added\u00a0<\/span><span data-contrast=\"auto\">a new\u00a0<\/span><span data-contrast=\"auto\">C++ Core Check rule<\/span><span data-contrast=\"auto\">, suggesting a way to remove the copy<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">void\u00a0emailEveryoneInCompany(const std::vector&lt;Person&gt;&amp; employees) {\u00a0\r\n\u00a0\u00a0\u00a0 Email\u00a0email;\u00a0\r\n\u00a0\u00a0\u00a0 for (Person p: employees)\u00a0{ \/\/ Warning C26817: Potentially expensive copy of variable 'p' in range-for loop. Consider making it a const reference (es.71)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0email.addRecipient(p.email_address);\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0email.addBody(\"Sorry for the spam\");\u00a0\r\n\u00a0\u00a0\u00a0\u00a0email.send();\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">By using the suggestion from the warning and changing the type of the variable <code>p<\/code> in the<\/span><span data-contrast=\"auto\"> loop from a <code>Person<\/code> to a <code>const Person&amp;<\/code>, the variable no longer receives an expensive copy of the data on each iteration.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">void\u00a0emailEveryoneInCompany(const std::vector&lt;Person&gt;&amp; employees) {\u00a0\r\n\u00a0\u00a0\u00a0 Email\u00a0email;\u00a0\r\n\u00a0\u00a0\u00a0 for (const Person&amp; p: employees)\u00a0{ \/\/ expensive copy no longer occurs\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0email.addRecipient(p.email_address);\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0email.addBody(\"Sorry for the spam\");\u00a0\r\n\u00a0\u00a0\u00a0\u00a0email.send();\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">To decide what constitutes an &#8220;expensive&#8221; copy, the following heuristic is used by the che<\/span><span data-contrast=\"auto\">ck:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">If the size of the type is greater than twice the platform-dependent pointer size and the type is not a smart pointer or one of <\/span><code><span data-contrast=\"auto\">gsl<\/span><\/code><span data-contrast=\"auto\"><code>::span<\/code>, <\/span><code><span data-contrast=\"auto\">gsl<\/span><span data-contrast=\"auto\">::<\/span><span data-contrast=\"auto\">string_span<\/span><\/code><span data-contrast=\"auto\">, or <code>std::<\/code><\/span><code><span data-contrast=\"auto\">string_view<\/span><\/code><span data-contrast=\"auto\">, then the copy is considered expensive. This means that for small\u00a0<\/span><span data-contrast=\"auto\">datatypes<\/span><span data-contrast=\"auto\"> such as numeric scalars, the warning will not trigger. For larger types, such as the <code>Person<\/code> type in the example above, the copy is considered expensive and a warning will be raised.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">One final point to note is that the check will not fire if t<\/span><span data-contrast=\"auto\">he variable is mutated in the loop body<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">struct Person {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::string\u00a0first_name;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::string\u00a0last_name;\u00a0\r\n\u00a0\u00a0\u00a0 int\u00a0hourlyrate; \/\/ dollars per hour\u00a0\r\n};\u00a0\r\n\r\nvoid\u00a0giveEveryoneARaise(const std::vector&lt;Person&gt;&amp; employees) {\u00a0\r\n\u00a0\u00a0\u00a0 for (Person p: employees) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0p.hourlyrate\u00a0+= 10; \/\/ `p` can no longer be marked `const Person&amp;`, so the copy is unavoidable\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">If instead the container\u00a0<\/span><span data-contrast=\"auto\">was not<\/span><span data-contrast=\"auto\"> const-qualified, then the copy could be avoided by changing the type <code>Person<\/code> to <code>Perso<\/code><\/span><span data-contrast=\"auto\"><code>n&amp;<\/code>.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">void\u00a0giveEveryoneARaise() {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::vector&lt;Person&gt; employees =\u00a0getEmployees();\u00a0\r\n\u00a0\u00a0\u00a0 for (Person&amp; p: employees)\u00a0{ \/\/ no more expensive copying, but any subsequent mutation will change the container!\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0p.hourlyrate\u00a0+= 10;\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">But this change comes with the introduction of new side-effects to the code. Therefore, the range-for copy warning only ever suggests\u00a0<\/span><span data-contrast=\"auto\">marking<\/span><span data-contrast=\"auto\"> the loop variable as <code>const T&amp;<\/code><\/span><span data-contrast=\"auto\">, and<\/span><span data-contrast=\"auto\"> will not fire if the loop-variable cannot legally be marked <code>const<\/code>.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Full documentation of the check can be found <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/code-quality\/c26817?view=vs-2019\">here<\/a><\/span><span data-contrast=\"auto\">.\u00a0<\/span><span data-contrast=\"auto\">This rule is enabled by default in Visual Studio<\/span><span data-contrast=\"auto\">\u00a0when you run code analysis.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<h2><span data-contrast=\"none\">Expensive copy with the <code>auto<\/code> keyword<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">The last new check in this release concerns itself with expensive copies\u00a0<\/span><span data-contrast=\"auto\">occurring<\/span><span data-contrast=\"auto\">\u00a0wit<\/span><span data-contrast=\"auto\">h the use of the <code>auto<\/code> type.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Consider the following Rust example in which type resolution occurs for a variable being assigned a reference.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">struct\u00a0PasswordManager\u00a0{\u00a0\r\n\u00a0\u00a0\u00a0 password: String\u00a0\r\n}\u00a0\r\n\r\nimpl\u00a0PasswordManager\u00a0{\u00a0\r\n\u00a0\u00a0\u00a0 \/\/ member-access function returning an immutable reference to a member\u00a0\r\n\u00a0\u00a0\u00a0\u00a0fn\u00a0getPassword(&amp;self) -&gt; &amp;String\u00a0{ &amp;self.password\u00a0}\u00a0\r\n\u00a0\u00a0\u00a0 \/\/ Note: for the sake of an example dealing with expensive types, a &amp;String is being returned.\u00a0\r\n\u00a0\u00a0\u00a0 \/\/ More realistically though, a string slice\u00a0would be returned instead (similar to\u00a0a string view in C++)\u00a0\r\n}\u00a0\r\n\r\nfn\u00a0stealPassword(pm: &amp;PasswordManager) {\u00a0\r\n\u00a0\u00a0\u00a0 let password =\u00a0pm.getPassword(); \/\/ the type of `a` resolves to `&amp;String`. No copy occurs.\u00a0\r\n}<\/pre>\n<p><span data-contrast=\"auto\">Because of Rust&#8217;s requirement that in\u00a0<\/span><span data-contrast=\"auto\">the<\/span><span data-contrast=\"auto\">\u00a0majority of<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">cases<\/span><span data-contrast=\"auto\"> copying must be explicit, the type of <code>password<\/code>\u00a0in the example automatically resolves to an immutable reference when being assigned an immutable reference, and no expensive copying is performed.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">On the other hand, consider the equiv<\/span><span data-contrast=\"auto\">alent C++ code<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">class PasswordManager {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0std::string password;\u00a0\r\npublic:\u00a0\r\n\u00a0\u00a0\u00a0 const std::string&amp; getPassword() const { return password; }\u00a0\u00a0\r\n};\u00a0\r\n\r\nvoid stealPassword(const PasswordManager&amp; pm) {\r\n\u00a0\u00a0\u00a0 auto password = pm.getPassword(); \/\/ the type of `password` resolves to `std::string`. Copy occurs.\r\n}<\/pre>\n<p><span data-contrast=\"auto\">Here, the type of <code>password<\/code> resolves to <\/span><code><span data-contrast=\"auto\">std::<\/span><\/code><span data-contrast=\"auto\"><code>string<\/code>, even though the return type of <\/span><code><span data-contrast=\"auto\">getPassword<\/span><\/code><span data-contrast=\"auto\"><code>()<\/code> is a\u00a0<\/span><span data-contrast=\"auto\">const-reference<\/span><span data-contrast=\"auto\"> to a string. The resulting behavior is that the contents of <\/span><code><span data-contrast=\"auto\">Pas<\/span><span data-contrast=\"auto\">swordManager<\/span><span data-contrast=\"auto\">::<\/span><\/code><span data-contrast=\"auto\"><code>password<\/code> get copied into the local variable <code>password<\/code>.<\/span><\/p>\n<p><span data-contrast=\"auto\">Compare this with a function returning a pointer:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">class\u00a0PasswordManager\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0std::string password;\r\npublic:\r\n\u00a0\u00a0\u00a0 const\u00a0std::string*\u00a0getPassword() const { return &amp;password; }\r\n};\r\n\r\nvoid\u00a0stealPassword(const\u00a0PasswordManager&amp; pm) {\r\n\u00a0\u00a0\u00a0 auto password =\u00a0pm.getPassword(); \/\/ the type of `password` resolves to `const std::string*`. No copy occurs.\r\n}<\/pre>\n<p><span data-contrast=\"auto\">This difference in behavior between assigning a reference and a point<\/span><span data-contrast=\"auto\">er to a variable marked <code>auto<\/code> is non-obvious, resulting in potentially unwanted and unexpected copying.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To guard against bugs arising from this behavior, the checker examines all instances of initialization from a reference to a variable marked <code>auto<\/code>.<\/span><span data-contrast=\"auto\"> If the resulting copy is deemed expensive using the same heuristics as in the range-for check, the checker warns to mark the variable <code>const auto&amp;<\/code> instead.<\/span><\/p>\n<pre class=\"prettyprint\">class\u00a0PasswordManager\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0std::string password;\r\npublic:\r\n\u00a0\u00a0\u00a0 const\u00a0std::string&amp;\u00a0getPassword() const { return password; }\r\n};\r\n\r\nvoid\u00a0stealPassword(const\u00a0PasswordManager&amp; pm) {\r\n\u00a0\u00a0\u00a0 auto password =\u00a0pm.getPassword();\u00a0 \/\/ Warning C26820: Assigning by value when a const-reference would suffice, use const\u00a0auto&amp;amp; instead (p.9)\r\n}<\/pre>\n<p><span data-contrast=\"auto\">A<\/span><span data-contrast=\"auto\">nd as with the range-for check, this warning does not get raised whenever the variable cannot be legally marked <code>const<\/code><\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">std::string\u00a0hashPassword(const\u00a0PasswordManager&amp; pm) {\r\n\u00a0\u00a0\u00a0 auto password =\u00a0pm.getPassword(); \/\/ warning no longer gets raised because `password` is modified below\r\n\u00a0\u00a0\u00a0 password += \"salt\";\r\n\u00a0\u00a0\u00a0 return\u00a0std::hash(password);\r\n}<\/pre>\n<p><span data-contrast=\"auto\">Another instance in which the warning does not get raised is whenever the reference is being derived from a temporary. In such cases, using <code>const a<\/code><\/span><span data-contrast=\"auto\"><code>uto&amp;<\/code> would<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">result in a dangling reference once the temporary gets destroyed.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">class\u00a0PasswordManager\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0std::string password;\r\npublic:\r\n\u00a0\u00a0\u00a0\u00a0PasswordManager(const std::string&amp; password): password(password) {}\r\n\u00a0\u00a0\u00a0 const\u00a0std::string&amp;\u00a0getPassword() const { return password; }\r\n};\r\n\r\nvoid\u00a0stealPassword() {\r\n\u00a0\u00a0\u00a0 const auto&amp; password =\u00a0PasswordManager(\"123\").getPassword(); \/\/ using `const auto&amp;` instead of just `auto`\r\n\u00a0\u00a0\u00a0\u00a0use_password(password); \/\/ `password` is now referencing invalid memory!\r\n}<\/pre>\n<p><span data-contrast=\"auto\">Full documentation of the check can be found <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/code-quality\/c26820?view=vs-2019\">here<\/a><\/span><span data-contrast=\"auto\">.\u00a0<\/span><span data-contrast=\"auto\">This rule is enabled by default in Visual Studio<\/span><span data-contrast=\"auto\">\u00a0when you run code analysis.<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Give us your feedback<\/span><\/h2>\n<p><span data-contrast=\"auto\">Check out these newly added rules and the recently released\u00a0<\/span><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/gsl-3-0-0-release\/\"><span data-contrast=\"none\">GSL 3.0<\/span><\/a><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">library\u00a0<\/span><span data-contrast=\"auto\">and let us know if they\u00a0<\/span><span data-contrast=\"auto\">help<\/span><span data-contrast=\"auto\"> you write safer C++. Stay tuned as we add more safety <\/span><span data-contrast=\"auto\">rules<\/span><span data-contrast=\"auto\">\u00a0in future releases of Visual Studio.<\/span><\/p>\n<p><span data-contrast=\"auto\">Download\u202f<\/span><a href=\"https:\/\/visualstudio.microsoft.com\/vs\/\"><span data-contrast=\"none\">Visual Studio 2019 version 16.7<\/span><\/a><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">today and give it a try.\u00a0<\/span><span data-contrast=\"auto\">We would<\/span><span data-contrast=\"auto\">\u00a0love to hear from you to help us prioritize and build the right features for you. We can be reached via the comments below,\u202f<\/span><a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/8\/index.html\"><span data-contrast=\"none\">Developer Community<\/span><\/a><span data-contrast=\"auto\">,\u202fand Twitter (<\/span><a href=\"https:\/\/twitter.com\/visualc\"><span data-contrast=\"none\">@VisualC<\/span><\/a><span data-contrast=\"auto\">). The best way to file a bug or suggest a feature is via Developer Community.<\/span><span data-ccp-props=\"{&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rust and C++ are two popular systems programming languages.\u00a0For years, the focus of C++ has been on performance.\u00a0We are increasingly hearing calls from customers and security researchers that C++ should have stronger safety guarantees in the language.\u00a0C++ often falls behind Rust when it comes to programming safety.\u00a0Visual Studio 2019 version 16.7\u00a0contains\u00a0four new rules\u00a0in C++ Core [&hellip;]<\/p>\n","protected":false},"author":17037,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[119,245,163],"class_list":["post-26461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-code-analysis","tag-cppcorecheck","tag-static-analysis"],"acf":[],"blog_post_summary":"<p>Rust and C++ are two popular systems programming languages.\u00a0For years, the focus of C++ has been on performance.\u00a0We are increasingly hearing calls from customers and security researchers that C++ should have stronger safety guarantees in the language.\u00a0C++ often falls behind Rust when it comes to programming safety.\u00a0Visual Studio 2019 version 16.7\u00a0contains\u00a0four new rules\u00a0in C++ Core [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/26461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/17037"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=26461"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/26461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=26461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=26461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=26461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}