{"id":34525,"date":"2024-08-22T18:12:24","date_gmt":"2024-08-22T18:12:24","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=34525"},"modified":"2024-08-22T18:12:24","modified_gmt":"2024-08-22T18:12:24","slug":"prevent-critical-bugs-with-msvc-code-analysis","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/prevent-critical-bugs-with-msvc-code-analysis\/","title":{"rendered":"Prevent Critical Bugs with MSVC Code Analysis"},"content":{"rendered":"<p><span data-contrast=\"none\">Imagine this: You\u2019re deep into a complex C++ project, and everything seems to be running smoothly. But then, out of nowhere, a critical bug surfaces\u2014 one that requires a bit more foresight. We\u2019ve all been there, right? This is where code analysis steps in as your silent guardian.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/code-quality\/code-analysis-for-c-cpp-overview?view=msvc-170\"><span data-contrast=\"none\">Code analysis<\/span><\/a><span data-contrast=\"none\"> is a great tool for catching those elusive bugs and ensuring your code adheres to the best programming practices. It identifies defects that are difficult to discover through testing by searching for specific code patterns known to cause problems. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\"> The analysis results are displayed in the Visual Studio Error List window and as squiggles in the editor. This feature checks for problematic code patterns, such as buffer overruns caused by converting an element count into a byte count and null pointer dereferences, even if the code <\/span> <span data-contrast=\"none\">looks correct. In this blog, we will focus on MSVC Code Analysis, which is one of the different types of code analysis available in Visual Studio for C++.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span class=\"TextRun SCXW227821436 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun CommentStart SCXW227821436 BCX8\" data-ccp-parastyle=\"heading 1\">Where MSVC Code Analysis Shines<\/span><\/span><span class=\"EOP SCXW227821436 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:360,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">In 2014, the tech world was shaken by the discovery of the Heartbleed bug in OpenSSL. This critical vulnerability, caused by a missing bounds check, allowed attackers to exploit the TLS heartbeat extension and read sensitive data from server memory, including private keys, usernames, and passwords. The fallout was massive, affecting millions of users and causing widespread panic.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Now, picture yourself as a C++ developer working on a high-stakes project. You know that even a small mistake can lead to significant security vulnerabilities, just like Heartbleed. This is where MSVC Code Analysis becomes your best ally.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">MSVC Code Analysis is a static analysis tool that checks your code for errors, potential improvements, and adherence to coding best practices when using the Microsoft Visual C++ (MSVC) compiler. For example, failing to initialize a pointer (e.g.,<\/span> <code class=\"language-cpp\">int* uninitializedPtr;<\/code><span data-contrast=\"auto\">) in your project can result in unpredictable behavior, crashes, and security vulnerabilities. Consider the following scenario: <\/span><span data-contrast=\"auto\">You declare a pointer and initialize it to nullptr (<\/span><code class=\"language-cpp\">int* imageData = nullptr;<\/code><span data-contrast=\"auto\">). Later, you attempt to allocate memory for the pointer based on uninitialized width and height <\/span><span data-contrast=\"auto\">variables (<\/span><code class=\"language-cpp\">imageData = new int[width * height];<\/code><span data-contrast=\"auto\">).<\/span><span data-contrast=\"auto\"> This can lead to undefined behavior because <code class=\"language-cpp\">width<\/code> and <code class=\"language-cpp\">height<\/code> are not initialized before use. If the pointer is used before being properly assigned, it can lead to accessing uninitialized memory, which Rule C6001 identifies, helping you catch these issues before they become critical problems. The following sample generates \u2018<strong>Using uninitialized memory<\/strong>\u2019 warning:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint language-cpp\"><code class=\"language-cpp\">#include &lt;iostream&gt; \r\n\r\n#include &lt;stdexcept&gt; \r\n\r\n class ImageProcessor { \r\n\r\npublic: \r\n\r\n    void processImage() { \r\n\r\n        int width, height; \r\n\r\n        int* imageData = nullptr; \r\n\r\n        try { \r\n\r\n            \/\/ Attempt to allocate memory based on width and height \r\n\r\n            imageData = new int[width * height]; \/\/ Uninitialized width and height \r\n\r\n            \/\/ Process the image data (this will cause undefined behavior) \r\n\r\n            for (int i = 0; i &lt; width * height; ++i) { \r\n\r\n                imageData[i] = i; \/\/ Potentially accessing uninitialized memory \r\n\r\n            } \r\n\r\n            \/\/ Simulate further processing \r\n\r\n            std::cout &lt;&lt; \"Image processed successfully.\" &lt;&lt; std::endl; \r\n\r\n        } \r\n\r\n        catch (const std::bad_alloc&amp; e) { \r\n\r\n            std::cerr &lt;&lt; \"Memory allocation failed: \" &lt;&lt; e.what() &lt;&lt; std::endl; \r\n\r\n        } \r\n\r\n        \/\/ Clean up allocated memory \r\n\r\n        delete[] imageData; \r\n\r\n    } \r\n\r\n}; <\/code><\/pre>\n<p><span class=\"TrackedChange SCXW266567688 BCX8\"><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\">Now, <\/span><span class=\"NormalTextRun SCXW266567688 BCX8\">let\u2019s<\/span><span class=\"NormalTextRun SCXW266567688 BCX8\"> use this example to understand the <\/span><span class=\"NormalTextRun SCXW266567688 BCX8\">different ways<\/span><span class=\"NormalTextRun SCXW266567688 BCX8\"> to invoke <\/span><\/span><\/span><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\">c<\/span><\/span><span class=\"TrackedChange SCXW266567688 BCX8\"><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\">ode <\/span><\/span><\/span><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\">a<\/span><\/span><span class=\"TrackedChange SCXW266567688 BCX8\"><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\">nalysis<\/span><\/span><\/span><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\"> in Visual Studio<\/span><\/span><span class=\"TrackedChange SCXW266567688 BCX8\"><span class=\"TextRun SCXW266567688 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW266567688 BCX8\">.<\/span><\/span><\/span><\/p>\n<h3><span class=\"TrackedChange SCXW52097281 BCX8\"><span class=\"TextRun SCXW52097281 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW52097281 BCX8\" data-ccp-parastyle=\"heading 3\">Background Code Analysis<\/span><\/span><\/span><span class=\"TrackedChange SCXW52097281 BCX8\"><span class=\"TextRun SCXW52097281 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW52097281 BCX8\" data-ccp-parastyle=\"heading 3\">\u00a0<\/span><\/span><\/span><span class=\"EOP SCXW52097281 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">This integral feature of Visual Studio functions as a real-time code analysis tool. This tool is particularly beneficial for you because it:<\/span><span data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"33\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Provides immediate feedback on potential issues, aiding in early problem resolution.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"33\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">Focuses on the files that are currently open in the editor, streamlining analysis during active development. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><span data-contrast=\"none\">Background code analysis will automatically run after you open or save the file. The warning for uninitialized memory will be enabled by default, displaying a green squiggle in the editor and appearing in the error list, as demonstrated in the example below.<\/span><span data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/background-Code-Analysis.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34553\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/background-Code-Analysis.png\" alt=\"Image displaying background Code Analysis example\" width=\"1395\" height=\"775\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/background-Code-Analysis.png 1395w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/background-Code-Analysis-300x167.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/background-Code-Analysis-1024x569.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/background-Code-Analysis-768x427.png 768w\" sizes=\"(max-width: 1395px) 100vw, 1395px\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">This feature is enabled by default; <\/span><span class=\"NormalTextRun CommentStart SCXW229980447 BCX8\">however, you can<\/span> <span class=\"NormalTextRun SCXW229980447 BCX8\">double check <\/span><span class=\"NormalTextRun SCXW229980447 BCX8\">the setting by navigating to <\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">Tools <\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">&gt; <\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">Options <\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">&gt; <\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">Text Editor <\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">&gt;<\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\"> C\/C++<\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\"> &gt; <\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">Advanced <\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">&gt; <\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">Code Analysis<\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\"> &gt; <\/span><\/span><strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">Disable Background Code Analysis<\/span><\/span><\/strong><span class=\"TextRun SCXW229980447 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW229980447 BCX8\">: False.<\/span><\/span><\/p>\n<h3><span class=\"TrackedChange SCXW135980693 BCX8\"><span class=\"TextRun SCXW135980693 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW135980693 BCX8\" data-ccp-parastyle=\"heading 3\">Manually Running Code Analysis<\/span><\/span><\/span><span class=\"EOP SCXW135980693 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW134682869 BCX8\">In addition to background code analysis<\/span><span class=\"NormalTextRun SCXW134682869 BCX8\">,<\/span><span class=\"NormalTextRun SCXW134682869 BCX8\"> you can also <\/span><span class=\"NormalTextRun CommentStart SCXW134682869 BCX8\">manually run code analysis<\/span> <span class=\"NormalTextRun SCXW134682869 BCX8\">as <\/span><span class=\"NormalTextRun SCXW134682869 BCX8\">needed. <\/span><span class=\"NormalTextRun SCXW134682869 BCX8\">You can start by <\/span><span class=\"NormalTextRun CommentStart SCXW134682869 BCX8\">clearing<\/span><span class=\"NormalTextRun SCXW134682869 BCX8\"> all warnings in the <\/span><\/span><strong><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW134682869 BCX8\">current file<\/span><\/span><\/strong><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"> <span class=\"NormalTextRun SCXW134682869 BCX8\">you\u2019re<\/span><span class=\"NormalTextRun SCXW134682869 BCX8\"> working on, then invoke Code Analysis for the <\/span><\/span><strong><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW134682869 BCX8\">current project<\/span><\/span><\/strong><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW134682869 BCX8\">. Periodically, run Code Analysis for the <\/span><\/span><strong><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW134682869 BCX8\">entire solution<\/span><\/span><\/strong><span class=\"TextRun SCXW134682869 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW134682869 BCX8\"> to <\/span><span class=\"NormalTextRun SCXW134682869 BCX8\">maintain<\/span><span class=\"NormalTextRun SCXW134682869 BCX8\"> overall code quality<\/span><span class=\"NormalTextRun SCXW134682869 BCX8\">.<\/span> <span class=\"NormalTextRun SCXW134682869 BCX8\">You can also manually run code analysis for individual files.<\/span> <span class=\"NormalTextRun SCXW134682869 BCX8\">There are several ways to manually run a code analysis. Following any of these steps will display the \u2018Using uninitialized memory\u2019 warning in the error list window for our example.<\/span><\/span><span class=\"EOP SCXW134682869 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h4><span class=\"TextRun SCXW32697975 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW32697975 BCX8\" data-ccp-charstyle=\"Strong\">1. <\/span><span class=\"NormalTextRun SCXW32697975 BCX8\" data-ccp-charstyle=\"Strong\">Menu-bar:<\/span><\/span><span class=\"EOP SCXW32697975 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h4>\n<ol>\n<li><span class=\"TextRun SCXW80981315 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW80981315 BCX8\">Open <\/span><span class=\"NormalTextRun SCXW80981315 BCX8\">the project.<\/span><\/span><\/li>\n<li>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">From the <\/span><b><span data-contrast=\"none\">menu-bar<\/span><\/b><span data-contrast=\"none\">, select <\/span><b><span data-contrast=\"none\">Build <\/span><\/b><span data-contrast=\"none\">-&gt; <\/span><b><span data-contrast=\"none\">Run Code Analysis<\/span><\/b><span data-contrast=\"none\"> on [<\/span><b><span data-contrast=\"none\">Solution <\/span><\/b><span data-contrast=\"none\">|<\/span><b><span data-contrast=\"none\"> Project Name<\/span><\/b><span data-contrast=\"none\"> | <\/span><b><span data-contrast=\"none\">File<\/span><\/b><span data-contrast=\"none\">]<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"6\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">From the <\/span><b><span data-contrast=\"none\">menu-bar<\/span><\/b><span data-contrast=\"none\">, select <\/span><b><span data-contrast=\"none\">Analyze <\/span><\/b><span data-contrast=\"none\">-&gt; <\/span><b><span data-contrast=\"none\">Run Code Analysis<\/span><\/b><span data-contrast=\"none\"> -&gt; [<\/span><b><span data-contrast=\"none\">On Solution <\/span><\/b><span data-contrast=\"none\">| <\/span><b><span data-contrast=\"none\">Run Code Analysis on \u2018project name\u2019<\/span><\/b><span data-contrast=\"none\"> | <\/span><b><span data-contrast=\"none\">Run Code Analysis on File<\/span><\/b><span data-contrast=\"none\">]<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Code-Analysis-from-Analyze-Menu.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34555\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Code-Analysis-from-Analyze-Menu.png\" alt=\"Image showing Code Analysis from Analyze Menu\" width=\"973\" height=\"253\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Code-Analysis-from-Analyze-Menu.png 973w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Code-Analysis-from-Analyze-Menu-300x78.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Code-Analysis-from-Analyze-Menu-768x200.png 768w\" sizes=\"(max-width: 973px) 100vw, 973px\" \/><\/a><\/p>\n<h4><span class=\"TextRun SCXW177107939 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW177107939 BCX8\" data-ccp-charstyle=\"Strong\">2. <\/span><span class=\"NormalTextRun SCXW177107939 BCX8\" data-ccp-charstyle=\"Strong\">Keyboard Shortcut:<\/span><\/span><span class=\"EOP SCXW177107939 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:240,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h4>\n<ul>\n<li><span class=\"TextRun SCXW210910475 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW210910475 BCX8\">For a single file: <\/span><\/span><strong><span class=\"TextRun SCXW210910475 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW210910475 BCX8\">Ctrl + Shift + Alt + F7<\/span><\/span><span class=\"EOP SCXW210910475 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:240,&quot;335559740&quot;:279}\">\u00a0<\/span><\/strong><\/li>\n<\/ul>\n<p><span class=\"TextRun SCXW68786483 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW68786483 BCX8\">Learn more about the <\/span><span class=\"NormalTextRun SCXW68786483 BCX8\">different ways<\/span><span class=\"NormalTextRun SCXW68786483 BCX8\"> to run code analysis manually in <\/span><\/span><a class=\"Hyperlink SCXW68786483 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/code-quality\/quick-start-code-analysis-for-c-cpp?view=msvc-170#run-code-analysis\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW68786483 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW68786483 BCX8\" data-ccp-charstyle=\"Hyperlink\">Run code analysis<\/span><\/span><\/a><span class=\"TextRun SCXW68786483 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW68786483 BCX8\">.<\/span><\/span><span class=\"EOP SCXW68786483 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:240,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span class=\"TextRun SCXW88942987 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW88942987 BCX8\" data-ccp-parastyle=\"heading 3\">Enable Code Analysis on Build<\/span><\/span><span class=\"EOP SCXW88942987 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:240,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">This setup ensures that code analysis runs automatically every time you build your project or compile a single file. Think of code analysis as an inspector who checks your project. While this inspector might take more time to examine everything, catching potential issues early provides peace of mind.<\/span><span data-contrast=\"auto\"> This <\/span><span data-contrast=\"auto\">additional time you invest\u00a0 <\/span><span data-contrast=\"auto\"> is necessary to ensure your code is secure.<\/span> <span data-contrast=\"auto\">To enable it:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<ol>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"34\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">\u00a0<\/span><span data-contrast=\"none\">Open Project Properties in Visual Studio.<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"34\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Navigate to <\/span><b><span data-contrast=\"none\">Configuration Properties &gt; Code Analysis &gt; General<\/span><\/b><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"34\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Select the <\/span><b><span data-contrast=\"none\">Yes<\/span><\/b><span data-contrast=\"none\"> option for <\/span><b><span data-contrast=\"none\">Enable Code Analysis on Build.<\/span><\/b><span data-contrast=\"none\"> Note that this option is disabled by default, whereas Microsoft Code Analysis is enabled by default.<\/span>\n<ul>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"34\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">To view all default selected rules, including the rule for detecting uninitialized memory, navigate to <\/span><b><span data-contrast=\"none\">Configuration Properties<\/span><\/b><span data-contrast=\"none\"> &gt; <\/span><b><span data-contrast=\"none\">Code Analysis <\/span><\/b><span data-contrast=\"none\">&gt; <\/span><b><span data-contrast=\"none\">Microsoft<\/span><\/b><span data-contrast=\"none\">, and then click <\/span><b><span data-contrast=\"none\">Configure<\/span><\/b><span data-contrast=\"none\">.\u00a0\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Enable-Code-Analysis-on-Build-Setting.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34556\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Enable-Code-Analysis-on-Build-Setting.png\" alt=\"Image displaying Enable Code Analysis on Build Setting\" width=\"1159\" height=\"799\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Enable-Code-Analysis-on-Build-Setting.png 1159w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Enable-Code-Analysis-on-Build-Setting-300x207.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Enable-Code-Analysis-on-Build-Setting-1024x706.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Enable-Code-Analysis-on-Build-Setting-768x529.png 768w\" sizes=\"(max-width: 1159px) 100vw, 1159px\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW149037879 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW149037879 BCX8\">After enabling code analysis on build, building the example code will cause Visual Studio to generate a warning for the lines<\/span><\/span> <code class=\"language-cpp\">imageData = new int[width * height];<\/code><span class=\"TextRun SCXW149037879 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW149037879 BCX8\">,<\/span> <span class=\"NormalTextRun SCXW149037879 BCX8\">indicating<\/span><span class=\"NormalTextRun SCXW149037879 BCX8\"> that<span data-contrast=\"auto\"> <code class=\"language-cpp\">width<\/code> and <code class=\"language-cpp\">height<\/code> <\/span><\/span><span class=\"NormalTextRun SCXW149037879 BCX8\">are uninitialized. <\/span><\/span><span class=\"TrackedChange SCXW149037879 BCX8\"><span class=\"TextRun SCXW149037879 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW149037879 BCX8\">Background code analysis focuses on the files <\/span><span class=\"NormalTextRun SCXW149037879 BCX8\">you\u2019re<\/span><span class=\"NormalTextRun SCXW149037879 BCX8\"> actively working on, while build-time analysis ensures all project files are checked, catching any missed issues that <\/span><span class=\"NormalTextRun SCXW149037879 BCX8\">aren\u2019t<\/span><span class=\"NormalTextRun SCXW149037879 BCX8\"> in the current file<\/span><\/span><\/span><span class=\"TextRun SCXW149037879 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW149037879 BCX8\">. This warning will appear in the Error List window.<\/span><\/span><span class=\"EOP SCXW149037879 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/ImageProcessor_example.gif\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34536\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/ImageProcessor_example.gif\" alt=\"Gif showing MSVC code analysis running on build\" width=\"1530\" height=\"832\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW220067071 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW220067071 BCX8\">Key Events in Microsoft C++ Code Analysis help you quickly <\/span><span class=\"NormalTextRun SCXW220067071 BCX8\">identify<\/span><span class=\"NormalTextRun SCXW220067071 BCX8\"> and fix defects by providing detailed information in selected warnings<\/span><span class=\"NormalTextRun SCXW220067071 BCX8\"> from <\/span><span class=\"NormalTextRun SCXW220067071 BCX8\">the <\/span><span class=\"NormalTextRun SCXW220067071 BCX8\">Error List<\/span><span class=\"NormalTextRun SCXW220067071 BCX8\">. They trace code flow to pinpoint root causes, making it easier to understand issues like variable initialization or branching. For example, double-clicking the <strong>C6001 \u2018Using uninitialized memory: width\u2019<\/strong> warning in the Error List opens a new window showing the Key Events. <\/span><\/span><span class=\"TextRun SCXW220067071 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW220067071 BCX8\">For further insights, please refer to the <\/span><\/span><a class=\"Hyperlink SCXW220067071 BCX8\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/microsoft-cpp-code-analysis-warnings-with-key-events\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"FieldRange SCXW220067071 BCX8\"><span class=\"TextRun Underlined SCXW220067071 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW220067071 BCX8\" data-ccp-charstyle=\"Hyperlink\">Microsoft C++ Code Analysis Warnings with Key Events<\/span><\/span><\/span><\/a><span class=\"TextRun SCXW220067071 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW220067071 BCX8\"> blog.<\/span><\/span><span class=\"EOP SCXW220067071 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34558\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events.png\" alt=\"Image showing Key Events\" width=\"1587\" height=\"306\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events.png 1587w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events-300x58.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events-1024x197.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events-768x148.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/Key-Events-1536x296.png 1536w\" sizes=\"(max-width: 1587px) 100vw, 1587px\" \/><\/a><\/p>\n<h3><span class=\"NormalTextRun CommentStart SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">Code Analysis <\/span><span class=\"NormalTextRun SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">Rules and <\/span><span class=\"NormalTextRun SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">R<\/span><span class=\"NormalTextRun CommentStart SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">ule<\/span><span class=\"NormalTextRun SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">s<\/span><span class=\"NormalTextRun SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">et<\/span><span class=\"NormalTextRun SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">s<\/span> <span class=\"NormalTextRun SCXW74181844 BCX8\" data-ccp-parastyle=\"heading 2\">\u00a0<\/span><\/h3>\n<p><span class=\"TrackedChange SCXW176653324 BCX8\"><span class=\"TextRun SCXW176653324 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun CommentStart SCXW176653324 BCX8\">Rulesets<\/span><\/span><\/span><span class=\"TrackedChange SCXW176653324 BCX8\"><span class=\"TextRun SCXW176653324 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW176653324 BCX8\"> in Visual Studio for C++ are collections of code analysis rules that ensure code quality and adherence to standards<\/span><\/span><\/span><span class=\"TextRun SCXW176653324 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW176653324 BCX8\">. <\/span><span class=\"NormalTextRun CommentStart SCXW176653324 BCX8\">For example<\/span><span class=\"NormalTextRun SCXW176653324 BCX8\">, <\/span><span class=\"NormalTextRun CommentStart SCXW176653324 BCX8\">enabling the MSVC rule <strong>C26440<\/strong><\/span><strong><span class=\"NormalTextRun SCXW176653324 BCX8\">, <\/span><span class=\"NormalTextRun SCXW176653324 BCX8\">\u2018Function can be declared \u2018<\/span><span class=\"NormalTextRun SpellingErrorV2Themed SCXW176653324 BCX8\">noexcept<\/span><\/strong><span class=\"NormalTextRun SCXW176653324 BCX8\"><strong>\u2019,\u2019<\/strong> suggests marking functions with \u2018<\/span><span class=\"NormalTextRun SpellingErrorV2Themed SCXW176653324 BCX8\">noexcept<\/span><span class=\"NormalTextRun SCXW176653324 BCX8\">\u2019 if they do not throw exceptions. This can improve both performance and reliability<\/span><span class=\"NormalTextRun SCXW176653324 BCX8\">.<\/span><\/span><span class=\"EOP SCXW176653324 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:240,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span class=\"TextRun SCXW234673389 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW234673389 BCX8\">To create a new custom rule set with the \u201cFunction can be declared \u2018<\/span><span class=\"NormalTextRun SpellingErrorV2Themed SCXW234673389 BCX8\">noexcept<\/span><span class=\"NormalTextRun SCXW234673389 BCX8\">\u2019\u201d rule added, follow these steps:<\/span><\/span><span class=\"EOP SCXW234673389 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:240,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<ol>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Open <\/span><b><span data-contrast=\"none\">Project Properties<\/span><\/b><span data-contrast=\"none\"> in Visual Studio <\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Navigate to <\/span><b><span data-contrast=\"none\">Configuration Properties &gt; Code Analysis &gt; <\/span><\/b><b><span data-contrast=\"auto\">Microsoft<\/span><\/b><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"auto\">In the<\/span><b><span data-contrast=\"auto\"> Active rules <\/span><\/b><span data-contrast=\"auto\">section, click \u201c<\/span><b><span data-contrast=\"auto\">Configure<\/span><\/b><span data-contrast=\"auto\">\u201d <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\">Select the check box for the\u00a0rule that you want to include in the ruleset. The Action will automatically change from <b style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\"><span data-contrast=\"auto\">\u2018None\u2019 <\/span><\/b><span style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\" data-contrast=\"auto\">to <\/span><b style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\"><span data-contrast=\"auto\">\u2018Warning\u2019<\/span><\/b><span style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\" data-contrast=\"auto\">. You can change the severity of this rule based on your needs, with options like Error, Info, Hidden, None, or &lt;Inherit&gt;. <\/span><span style=\"font-size: 1rem; text-align: var(--bs-body-text-align);\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Aptos\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:0,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769242&quot;:[65533,0],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;%1.&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span class=\"TextRun SCXW70220677 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW70220677 BCX8\">Save the rule set with a new file name. The custom rule set is now <\/span><span class=\"NormalTextRun CommentStart SCXW70220677 BCX8\">automatically assigned<\/span><span class=\"NormalTextRun SCXW70220677 BCX8\"> to the project.<\/span><\/span><span class=\"EOP SCXW70220677 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/li>\n<\/ol>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/CA_rule.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34551\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/CA_rule.png\" alt=\"Image showing Code Analysis rules\" width=\"1278\" height=\"436\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/CA_rule.png 1278w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/CA_rule-300x102.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/CA_rule-1024x349.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/08\/CA_rule-768x262.png 768w\" sizes=\"(max-width: 1278px) 100vw, 1278px\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW132631001 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW132631001 BCX8\">To learn more about using rule sets in depth, refer to the <\/span><\/span><a class=\"Hyperlink SCXW132631001 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/code-quality\/using-rule-sets-to-specify-the-cpp-rules-to-run?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW132631001 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW132631001 BCX8\" data-ccp-charstyle=\"Hyperlink\">Use Rule Sets to Specify the C++ Rules to Run<\/span><\/span><\/a><span class=\"TextRun SCXW132631001 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW132631001 BCX8\"> article.<\/span><span class=\"NormalTextRun SCXW132631001 BCX8\">\u00a0<\/span><\/span><span class=\"EOP TrackedChange SCXW132631001 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span class=\"TextRun SCXW181654809 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun CommentStart SCXW181654809 BCX8\" data-ccp-parastyle=\"heading 2\">Additional Tools and Techniques<\/span><\/span><\/h3>\n<p><span class=\"TextRun SCXW174501084 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW174501084 BCX8\">Visual Studio offers several features that can enhance code quality and prevent issues like the Heartbleed bug. Here are some tools you might find useful:<\/span><\/span><span class=\"EOP SCXW174501084 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h4><span class=\"FieldRange SCXW141823079 BCX8\"><span class=\"TextRun Underlined SCXW141823079 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun CommentStart SCXW141823079 BCX8\" data-ccp-charstyle=\"Hyperlink\">Clang-Tidy Code Analysis<\/span><\/span><\/span><span class=\"EOP SCXW141823079 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h4>\n<p><span class=\"TextRun SCXW201964196 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW201964196 BCX8\">Clang-Tidy, a tool used with the LLVM\/clang-cl compiler, is designed to modernize your code, ensure adherence to standards, perform static analysis, and automatically format your code. When using an MSVC toolset, you can configure Clang-Tidy to complement or replace the conventional Code Analysis process. This helps catch <\/span><span class=\"NormalTextRun SCXW201964196 BCX8\">different types<\/span><span class=\"NormalTextRun SCXW201964196 BCX8\"> of issues and improves overall code quality.<\/span><span class=\"NormalTextRun SCXW201964196 BCX8\"> You can find more details in <\/span><\/span><a class=\"Hyperlink SCXW201964196 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/code-quality\/clang-tidy?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW201964196 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW201964196 BCX8\" data-ccp-charstyle=\"Hyperlink\">Using Clang-Tidy in Visual Studio<\/span><\/span><\/a><span class=\"TextRun SCXW201964196 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"> <span class=\"NormalTextRun SCXW201964196 BCX8\">article<\/span><\/span><span class=\"TextRun SCXW201964196 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW201964196 BCX8\">.<\/span><\/span><span class=\"EOP SCXW201964196 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h4><span class=\"TextRun Underlined SCXW55448385 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW55448385 BCX8\" data-ccp-charstyle=\"Hyperlink\">Suppress Specific Warnings<\/span><\/span><span class=\"EOP SCXW55448385 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"none\">Suppressing specific warnings in C++ involves configuring your project settings to ignore certain compiler warnings<\/span><span data-contrast=\"none\"> for a single line, section of code, file, or entire project<\/span><span data-contrast=\"none\">. This can be done at the project level or for individual files using Visual Studio\u2019s project properties or \u2018<\/span><a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/preprocessor\/warning?view=msvc-170&amp;viewFallbackFrom=msvc-170).\"><span data-contrast=\"none\">#pragma warning\u2019<\/span><\/a><span data-contrast=\"none\"> directives. By suppressing less relevant warnings, you can focus on the most critical issues, making the build output cleaner and easier to read, which simplifies identifying and addressing significant problems. To dive deeper into this topic, check out the <\/span><a href=\"https:\/\/learn.microsoft.com\/en-us\/visualstudio\/ide\/how-to-suppress-compiler-warnings?view=vs-2022&amp;viewFallbackFrom=vs-2022%22%20%5Cl%20%22suppress-specific-warnings-for-c\"><span data-contrast=\"none\">Suppress compiler warnings<\/span><\/a><span data-contrast=\"none\"> article.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">It\u2019s important to note that Code Analysis<\/span> <span data-contrast=\"none\">tools may occasionally generate false positives. If you encounter a false positive, please report it through the <\/span><a href=\"https:\/\/developercommunity.visualstudio.com\/cpp\"><span data-contrast=\"none\">Visual Studio Developer Community<\/span><\/a><span data-contrast=\"none\"> channel with detailed repro code and information. This helps us to improve the accuracy of Code Analysis tools and ensures a smoother development experience.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span class=\"TextRun SCXW164900013 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW164900013 BCX8\" data-ccp-charstyle=\"Heading 2 Char\">Learn More<\/span><\/span><span class=\"EOP SCXW164900013 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">To learn more about securing your C++ programs, visit<\/span><span data-contrast=\"auto\"> the <\/span><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/build-reliable-and-secure-cpp-programs-microsoft-learn\/\"><span data-contrast=\"none\">Build Reliable and Secure C++ programs<\/span><\/a><span data-contrast=\"auto\"> blog. For the latest updates to the MSVC backend, check out the <\/span><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/msvc-backend-updates-in-visual-studio-2022-version-17-10\/\"><span data-contrast=\"none\">MSVC Backend Updates in Visual Studio 2022 version 17.10<\/span><\/a><span data-contrast=\"auto\"> blog.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Your feedback is invaluable in helping us enhance the MSVC Code Analysis experience. <\/span><span data-contrast=\"none\">Please share your suggestions in the comments below or through the<\/span> <a href=\"https:\/\/developercommunity.visualstudio.com\/cpp\"><span data-contrast=\"none\">Developer Community<\/span><\/a><span data-contrast=\"auto\">. <\/span><span data-contrast=\"none\">You can also reach us via email at<\/span> <a href=\"mailto:visualcpp@microsoft.com\"><span data-contrast=\"none\">visualcpp@microsoft.com<\/span><\/a><span data-contrast=\"none\"> or via X at <\/span><a href=\"https:\/\/x.com\/visualc\"><span data-contrast=\"none\">@VisualC<\/span><\/a><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335557856&quot;:16777215,&quot;335559738&quot;:180,&quot;335559739&quot;:0,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine this: You\u2019re deep into a complex C++ project, and everything seems to be running smoothly. But then, out of nowhere, a critical bug surfaces\u2014 one that requires a bit more foresight. We\u2019ve all been there, right? This is where code analysis steps in as your silent guardian.\u00a0 Code analysis is a great tool for [&hellip;]<\/p>\n","protected":false},"author":96741,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-34525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Imagine this: You\u2019re deep into a complex C++ project, and everything seems to be running smoothly. But then, out of nowhere, a critical bug surfaces\u2014 one that requires a bit more foresight. We\u2019ve all been there, right? This is where code analysis steps in as your silent guardian.\u00a0 Code analysis is a great tool for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/34525","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\/96741"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=34525"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/34525\/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=34525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=34525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=34525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}