{"id":35604,"date":"2025-07-15T18:00:13","date_gmt":"2025-07-15T18:00:13","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=35604"},"modified":"2025-07-15T18:58:19","modified_gmt":"2025-07-15T18:58:19","slug":"updates-to-warning-suppressions-in-microsoft-c-code-analysis","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/updates-to-warning-suppressions-in-microsoft-c-code-analysis\/","title":{"rendered":"Updates to Warning Suppressions in Microsoft C++ Code Analysis"},"content":{"rendered":"<p>Microsoft C++ Code Analysis is a powerful static analysis tool integrated into Visual\nStudio that helps you identify and fix potential issues in your C++ code.\nLarge projects require effective management of analysis warnings to maintain code\nquality and you sometimes need to suppress warnings, but must do so in a clear and\nauditable way.<\/p>\n<p>To that end, we are excited to announce significant updates to the warning suppression\nmechanisms in Microsoft C++ Code Analysis. These enhancements provide better tracking,\njustification, and overall management of warning suppressions, leading to a more\nmaintainable and robust codebase.<\/p>\n<h2>What&#8217;s New in the SARIF Output?<\/h2>\n<p>We enhanced the\n<a href=\"https:\/\/sarif.info\/\">Static Analysis Results Interchange Format (SARIF)<\/a>\noutput to include detailed information about warning suppressions, most notably the\njustification provided by you. This allows teams to easily review <em>why<\/em> a specific\nwarning was silenced.<\/p>\n<p>To generate a SARIF file, use the <code>\/analyze:log:format:sarif<\/code> compiler option. To ensure\nthat details about suppressed warnings (including their justifications) are included in\nthis SARIF log, you must also use the <code>\/analyze:log:includesuppressed<\/code> option.<\/p>\n<h2>What&#8217;s New in <code>gsl::suppress<\/code>?<\/h2>\n<p>We have updated our support for <code>gsl::suppress<\/code> to align with the latest C++ Core\nGuidelines syntax. Warnings can now be suppressed by using the <code>gsl::suppress<\/code> attribute\nin the following way:<\/p>\n<pre><code class=\"language-cpp\">[[gsl::suppress( \"&lt;warning_id&gt;\", justification: \"&lt;justification&gt;\" )]]<\/code><\/pre>\n<p>Where <code>&lt;warning_id&gt;<\/code> is the ID of the warning you want to suppress, and the optional\n<code>&lt;justification&gt;<\/code> is a string that provides a justification for the suppression.<\/p>\n<p><em>For now, <code>[[gsl::suppress]]<\/code> is only available for C++ codebases. For C codebases, you\nmust use the <code>#pragma warning(suppress)<\/code> syntax.<\/em><\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-cpp\">\/\/ CoreCheckExample.cpp\r\n\/\/ Add CppCoreCheck package and enable code analysis in build for warnings.\r\n\r\nint main()\r\n{\r\n    int arr[10];           \/\/ warning C26494\r\n    int* p = arr;          \/\/ warning C26485\r\n\r\n    [[gsl::suppress(\"bounds.1\", justification : \"This attribute suppresses Bounds rules #1\")]]\r\n    {\r\n        int* q = p + 1;    \/\/ warning C26481 (suppressed)\r\n        p = q++;           \/\/ warning C26481 (suppressed)\r\n    }\r\n\r\n    return 0;\r\n}<\/code><\/pre>\n<h2>What&#8217;s New in <code>#pragma warning<\/code>?<\/h2>\n<p>We have extended <code>#pragma warning<\/code> to support the <code>justification<\/code> field. Here is how\nyou can use it starting in Visual Studio 2022 version 17.14:<\/p>\n<pre><code class=\"language-cpp\">#pragma warning(suppress : &lt;warning_id&gt;, justification : \"&lt;justification&gt;\")<\/code><\/pre>\n<p>Where <code>&lt;warning_id&gt;<\/code> is the ID of the warning you want to suppress, and the optional\n<code>&lt;justification&gt;<\/code> is a string that provides a justification for the suppression.<\/p>\n<h2>Choosing Between <code>#pragma warning<\/code> and <code>gsl::suppress<\/code><\/h2>\n<p>Both <code>#pragma warning(suppress)<\/code> and <code>[[gsl::suppress]]<\/code> offer fine-grained control over\nwarning suppression.<\/p>\n<ul>\n<li><code>#pragma warning(suppress)<\/code> is a general MSVC mechanism that can be used for any\ncompiler warning. It&#8217;s particularly useful when you need to suppress a warning in a\nspecific code block without altering the code&#8217;s structure significantly.<\/li>\n<li><code>[[gsl::suppress]]<\/code> will only suppress warnings emitted by Microsoft C++ Code\nAnalysis. It is intended for use with the C++ Core Guidelines checks and can be applied\nto a scope or a specific declaration.<\/li>\n<\/ul>\n<p>Whenever possible, we recommend using <code>[[gsl::suppress]]<\/code> for suppressing Microsoft C++\nCode Analysis warnings.<\/p>\n<h2>Why These Updates Matter<\/h2>\n<p>These enhancements to warning suppression offer several key benefits:<\/p>\n<ul>\n<li><strong>Improved Auditability and Review:<\/strong> With justifications recorded directly in the\ncode and optionally in SARIF logs, code reviews become more effective. Team members can\nquickly understand the rationale behind a suppression without needing to consult\nexternal documentation or the original author.<\/li>\n<li><strong>Enhanced Code Maintainability:<\/strong> Clear justifications prevent accidental\nre-introduction of issues when code is refactored or suppressions are reviewed. They\nprovide a history of why certain warnings were deemed acceptable at a particular point.<\/li>\n<li><strong>Better Management of Technical Debt:<\/strong> Suppressed warnings can be a form of\ntechnical debt. Justifications help in tracking and prioritizing which suppressions\nshould be revisited and potentially fixed.<\/li>\n<li><strong>Consistency Across Suppression Mechanisms:<\/strong> By adding justification support to both\n<code>#pragma warning<\/code> and <code>gsl::suppress<\/code>, we provide a consistent experience for you. This\nallows old code to use <code>#pragma warning<\/code> while new code can use <code>[[gsl::suppress]]<\/code>, all\nwhile maintaining the ability to provide justifications.<\/li>\n<\/ul>\n<h3>Impact on Existing Workflows<\/h3>\n<p>These new features are additive. Existing suppression mechanisms (without\njustifications) will continue to work as before. However, we encourage you to\nstart using the <code>justification<\/code> attribute for new suppressions and to gradually update\nexisting ones where clarity is beneficial. There is no automatic migration, but the\nprocess of adding justifications is straightforward.<\/p>\n<h3>Availability<\/h3>\n<p>These enhancements are available in the MSVC compiler toolset shipping with Visual\nStudio 2022 version 17.14 and newer, and will be part of future Visual Studio releases.\nEnsure your Visual Studio is updated to leverage these improvements.<\/p>\n<h2>Try It Out<\/h2>\n<pre><code class=\"language-c++\">\/\/ example.cpp\r\n\/\/ Compile with: cl \/analyze:only \/analyze:plugin EspxEngine.dll \/analyze:log:format:sarif \/analyze:log:includesuppressed example.cpp\r\n\r\nint main()\r\n{\r\n    int arr[10];           \/\/ warning C26494\r\n    int* p = arr;          \/\/ warning C26485\r\n\r\n    [[gsl::suppress(\"bounds.1\", justification : \"This attribute suppresses Bounds rules #1\")]]\r\n    {\r\n        int* q = p + 1;    \/\/ warning C26481 (suppressed)\r\n        p = q++;           \/\/ warning C26481 (suppressed)\r\n    }\r\n\r\n    return 0;\r\n}<\/code><\/pre>\n<p>You can run the above code with the following options:\n<code>\/analyze \/analyze:log:format:sarif \/analyze:log:includesuppressed<\/code> to generate a SARIF\nfile that includes the suppression details.\n(Note: <code>EspxEngine.dll<\/code> is the plugin that enables C++ Core Guidelines checks, which are\nthe target of <code>gsl::suppress<\/code>.)<\/p>\n<pre><code class=\"language-powershell\">&gt; cl \/analyze:only \/analyze:plugin EspxEngine.dll \/analyze:log:format:sarif \/analyze:log:includesuppressed .\\example.cpp\r\nMicrosoft (R) C\/C++ Optimizing Compiler Version 19.50.35305.95 for x64\r\nCopyright (C) Microsoft Corporation.  All rights reserved.\r\n\r\nexample.cpp\r\nD:\\tmp\\example.cpp(7) : warning C26485: Expression 'arr': No array to pointer decay (bounds.3).\r\nD:\\tmp\\example.cpp(6) : warning C26494: Variable 'arr' is uninitialized. Always initialize an object (type.5).<\/code><\/pre>\n<p>You will also find a file named <code>example.nativecodeanalysis.sarif<\/code>, you can open it with\nVSCode (don&#8217;t forget to install the latest version of the\n<a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MS-SarifVSCode.sarif-viewer\">SARIF Viewer extension<\/a>).\nAfter filtering to include suppressed warnings, you will see the warning details which\ninclude the suppression information. Here is an example of what you will see:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-35609\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2.png\" alt=\"extension screenshot png image\" width=\"2500\" height=\"1359\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2.png 2500w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2-300x163.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2-1024x557.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2-768x417.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2-1536x835.png 1536w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2025\/07\/extension_screenshot-scaled_v2-2048x1113.png 2048w\" sizes=\"(max-width: 2500px) 100vw, 2500px\" \/><\/a><\/p>\n<h2>Feedback<\/h2>\n<p>We would love to hear your thoughts on the new changes to warning suppressions! Please\nshare your feedback and suggestions in the comments below. If you run into any issues,\nplease let us know by filing a feedback ticket on\n<a href=\"https:\/\/developercommunity.visualstudio.com\/cpp\">Visual Studio Developer Community<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Improvements to \\#pragma warning and gsl::suppress in Microsoft C++ Code Analysis.<\/p>\n","protected":false},"author":169849,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270,1],"tags":[119],"class_list":["post-35604","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-cplusplus","tag-code-analysis"],"acf":[],"blog_post_summary":"<p>Improvements to \\#pragma warning and gsl::suppress in Microsoft C++ Code Analysis.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/35604","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\/169849"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=35604"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/35604\/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=35604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=35604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=35604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}