CINXE.COM

Customizing static analysis | Dart

<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><script>!function(e,t,a,n){e[n]=e[n]||[],e[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var g=t.getElementsByTagName(a)[0],m=t.createElement(a);m.async=!0,m.src="https://www.googletagmanager.com/gtm.js?id=GTM-5VSZM5J",g.parentNode.insertBefore(m,g)}(window,document,"script","dataLayer")</script><meta name="description" content="Use an analysis options file and code comments to customize static analysis."><title>Customizing static analysis | Dart</title><link rel="icon" sizes="64x64" href="/assets/img/logo/dart-64.png" eleventy:ignore><link href="/assets/img/touch-icon-iphone.png" rel="apple-touch-icon" eleventy:ignore><link href="/assets/img/touch-icon-ipad.png" rel="apple-touch-icon" sizes="152x152" eleventy:ignore><link href="/assets/img/touch-icon-iphone-retina.png" rel="apple-touch-icon" sizes="180x180" eleventy:ignore><link href="/assets/img/touch-icon-ipad-retina.png" rel="apple-touch-icon" sizes="167x167" eleventy:ignore><meta name="twitter:card" content="summary"><meta name="twitter:site" content="@dart_lang"><meta name="twitter:title" content="Customizing static analysis"><meta name="twitter:description" content="Use an analysis options file and code comments to customize static analysis."><meta property="og:title" content="Customizing static analysis"><meta property="og:description" content="Use an analysis options file and code comments to customize static analysis."><meta property="og:url" content="/tools/analysis/"><meta property="og:image" content="/assets/img/logo/dart-logo-for-shares.png?2" eleventy:ignore><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Display:wght@400&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Mono:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0..1,0" rel="stylesheet"><link rel="stylesheet" href="/assets/css/main.css?v=3"><script src="/assets/js/os-tabs.js?v=3"></script><script src="/assets/js/main.js?v=3"></script><script>!function(e,a,t,n,c,o,s){e.GoogleAnalyticsObject=c,e[c]=e[c]||function(){(e[c].q=e[c].q||[]).push(arguments)},e[c].l=1*new Date,o=a.createElement(t),s=a.getElementsByTagName(t)[0],o.async=1,o.src="//www.google-analytics.com/analytics.js",s.parentNode.insertBefore(o,s)}(window,document,"script",0,"ga"),ga("create","UA-26406144-4","auto"),ga("send","pageview")</script></head><body class="default.html highlight-diagnostics"><a id="skip-to-main" class="filled-button" href="#site-content-title" tabindex="1">Skip to main content</a><section id="cookie-notice"><div class="container"><p>dart.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.</p><div class="button-group"><a class="text-button" href="https://policies.google.com/technologies/cookies" target="_blank" rel="noopener">Learn more</a> <button id="cookie-consent" class="filled-button">OK, got it</button></div></div></section><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5VSZM5J" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><div id="site-banner" role="alert"><p>Announcing Dart 3.7! Find out about updates to the language, analyzer, pub.dev, and more, in the <a href="https://medium.com/dartlang/announcing-dart-3-7-bf864a1b195c" target="_blank">blog post</a>.</p></div><header id="site-header"><nav id="mainnav"><div id="menu-toggle"><span class="material-symbols" title="Toggle side navigation menu." aria-label="Toggle side navigation menu." type="button">menu</span></div><a href="/" class="brand" title="Dart"><img src="/assets/img/logo/logo-white-text.svg" alt="Dart"></a><ul class="navbar"><li><a href="/overview" class="nav-link">Overview</a></li><li class="mainnav__get-started"><a href="/docs" class="nav-link active"><span>Docs</span></a></li><li><a href="/community" class="nav-link">Community</a></li><li><a href="/#try-dart" class="nav-link">Try Dart</a></li><li><a href="/get-dart" class="nav-link">Get Dart</a></li><li class="searchfield"><form action="/search" class="site-header-search form-inline" id="cse-search-box"><input type="hidden" name="cx" value="011220921317074318178:_yy-tmb5t_i"> <input type="hidden" name="ie" value="UTF-8"> <input type="hidden" name="hl" value="en"> <input class="site-header-searchfield form-control search-field" type="search" name="q" id="search-main" autocomplete="off" placeholder="Search" aria-label="Search"></form></li></ul></nav></header><div id="site-below-header"><div id="site-main-row"><div id="sidenav"><form action="/search/" class="site-header-search form-inline"><input class="site-header-searchfield form-control search-field" type="search" name="q" id="search-side" autocomplete="off" placeholder="Search" aria-label="Search"></form><ul class="navbar-nav"><li aria-hidden="true"><div class="sidenav-divider"></div></li><li class="nav-item"><a href="/overview" class="nav-link">Overview</a></li><li class="nav-item"><a href="/community" class="nav-link">Community</a></li><li class="nav-item"><a href="https://dartpad.dev" class="nav-link">Try Dart</a></li><li class="nav-item"><a href="/get-dart" class="nav-link">Get Dart</a></li><li class="nav-item"><a href="/docs" class="nav-link">Docs</a></li><li aria-hidden="true"><div class="sidenav-divider"></div></li></ul><ul class="nav"><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-1" role="button" aria-expanded="false" aria-controls="-sidenav-1"><span>Language</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1"><li class="nav-item"><a class="nav-link" href="/language"><div><span>Introduction</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-2" role="button" aria-expanded="false" aria-controls="-sidenav-1-2"><span>Syntax basics</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-2"><li class="nav-item"><a class="nav-link" href="/language/variables"><div><span>Variables</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/operators"><div><span>Operators</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/comments"><div><span>Comments</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/metadata"><div><span>Metadata</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/libraries"><div><span>Libraries & imports</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/keywords"><div><span>Keywords</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-3" role="button" aria-expanded="false" aria-controls="-sidenav-1-3"><span>Types</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-3"><li class="nav-item"><a class="nav-link" href="/language/built-in-types"><div><span>Built-in types</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/records"><div><span>Records</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/collections"><div><span>Collections</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/generics"><div><span>Generics</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/typedefs"><div><span>Typedefs</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/type-system"><div><span>Type system</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-4" role="button" aria-expanded="false" aria-controls="-sidenav-1-4"><span>Patterns</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-4"><li class="nav-item"><a class="nav-link" href="/language/patterns"><div><span>Overview & usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/pattern-types"><div><span>Pattern types</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://codelabs.developers.google.com/codelabs/dart-patterns-records" target="_blank" rel="noopener"><div><span>Applied tutorial</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li></ul></li><li class="nav-item"><a class="nav-link" href="/language/functions"><div><span>Functions</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-6" role="button" aria-expanded="false" aria-controls="-sidenav-1-6"><span>Control flow</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-6"><li class="nav-item"><a class="nav-link" href="/language/loops"><div><span>Loops</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/branches"><div><span>Branches</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/error-handling"><div><span>Error handling</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-7" role="button" aria-expanded="false" aria-controls="-sidenav-1-7"><span>Classes & objects</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-7"><li class="nav-item"><a class="nav-link" href="/language/classes"><div><span>Classes</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/constructors"><div><span>Constructors</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/methods"><div><span>Methods</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/extend"><div><span>Extend a class</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/mixins"><div><span>Mixins</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/enums"><div><span>Enums</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/extension-methods"><div><span>Extension methods</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/extension-types"><div><span>Extension types</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/callable-objects"><div><span>Callable objects</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-8" role="button" aria-expanded="false" aria-controls="-sidenav-1-8"><span>Class modifiers</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-8"><li class="nav-item"><a class="nav-link" href="/language/class-modifiers"><div><span>Overview & usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/class-modifiers-for-apis"><div><span>Class modifiers for API maintainers</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/modifier-reference"><div><span>Reference</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-9" role="button" aria-expanded="false" aria-controls="-sidenav-1-9"><span>Concurrency</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-9"><li class="nav-item"><a class="nav-link" href="/language/concurrency"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/async"><div><span>Asynchronous support</span></div></a></li><li class="nav-item"><a class="nav-link" href="/language/isolates"><div><span>Isolates</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-1-10" role="button" aria-expanded="false" aria-controls="-sidenav-1-10"><span>Null safety</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-1-10"><li class="nav-item"><a class="nav-link" href="/null-safety"><div><span>Sound null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/migration-guide"><div><span>Migrating to null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/understanding-null-safety"><div><span>Understanding null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/unsound-null-safety"><div><span>Unsound null safety</span></div></a></li><li class="nav-item"><a class="nav-link" href="/null-safety/faq"><div><span>FAQ</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-2" role="button" aria-expanded="false" aria-controls="-sidenav-2"><span>Core libraries</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-2"><li class="nav-item"><a class="nav-link" href="/libraries"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-core"><div><span>dart:core</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-async"><div><span>dart:async</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-math"><div><span>dart:math</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-convert"><div><span>dart:convert</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-io"><div><span>dart:io</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop"><div><span>dart:js_interop</span></div></a></li><div class="sidenav-divider"></div><li class="nav-item"><a class="nav-link" href="/libraries/collections/iterables"><div><span>Iterable collections</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-2-10" role="button" aria-expanded="false" aria-controls="-sidenav-2-10"><span>Asynchronous programming</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-2-10"><li class="nav-item"><a class="nav-link" href="/libraries/async/async-await"><div><span>Tutorial</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/futures-error-handling"><div><span>Futures and error handling</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/using-streams"><div><span>Using streams</span></div></a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/creating-streams"><div><span>Creating streams</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-3" role="button" aria-expanded="false" aria-controls="-sidenav-3"><span>Effective Dart</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-3"><li class="nav-item"><a class="nav-link" href="/effective-dart"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/style"><div><span>Style</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/documentation"><div><span>Documentation</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/usage"><div><span>Usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/design"><div><span>Design</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-4" role="button" aria-expanded="false" aria-controls="-sidenav-4"><span>Packages</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-4"><li class="nav-item"><a class="nav-link" href="/tools/pub/packages"><div><span>How to use packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/useful-packages"><div><span>Commonly used packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/create-packages"><div><span>Creating packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/publishing"><div><span>Publishing packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/writing-package-pages"><div><span>Writing package pages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/workspaces"><div><span>Workspaces (monorepo support)</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-4-7" role="button" aria-expanded="false" aria-controls="-sidenav-4-7"><span>Package reference</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-4-7"><li class="nav-item"><a class="nav-link" href="/tools/pub/dependencies"><div><span>Dependencies</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/glossary"><div><span>Glossary</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/package-layout"><div><span>Package layout conventions</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/environment-variables"><div><span>Pub environment variables</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/pubspec"><div><span>Pubspec file</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/troubleshoot"><div><span>Troubleshooting pub</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/verified-publishers"><div><span>Verified publishers</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/security-advisories"><div><span>Security advisories</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/versioning"><div><span>Versioning</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/custom-package-repositories"><div><span>Custom package repositories</span></div></a></li></ul></li><li class="nav-item"><a class="nav-link" href="/tools/pub/private-files"><div><span>What not to commit</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-5" role="button" aria-expanded="false" aria-controls="-sidenav-5"><span>Development</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-5"><li class="nav-item"><a class="nav-link" href="/libraries/serialization/json"><div><span>JSON serialization</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/language/number-representation"><div><span>Number representation</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/google-apis"><div><span>Google APIs</span></div></a></li><li class="nav-item"><a class="nav-link" href="/multiplatform-apps"><div><span>Multi-platform apps</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-5-5" role="button" aria-expanded="false" aria-controls="-sidenav-5-5"><span>Command-line & server apps</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-5-5"><li class="nav-item"><a class="nav-link" href="/server"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/get-started"><div><span>Get started</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/cmdline"><div><span>Write command-line apps</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/fetch-data"><div><span>Fetch data from the internet</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/httpserver"><div><span>Write HTTP servers</span></div></a></li><li class="nav-item"><a class="nav-link" href="/server/libraries"><div><span>Libraries & packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/server/google-cloud"><div><span>Google Cloud</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-5-6" role="button" aria-expanded="false" aria-controls="-sidenav-5-6"><span>Web apps</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-5-6"><li class="nav-item"><a class="nav-link" href="/web"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/get-started"><div><span>Get started</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/deployment"><div><span>Deployment</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/libraries"><div><span>Libraries & packages</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/wasm"><div><span>Wasm compilation</span></div></a></li></ul></li><li class="nav-item"><a class="nav-link" href="/libraries/core/environment-declarations"><div><span>Environment declarations</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-6" role="button" aria-expanded="false" aria-controls="-sidenav-6"><span>Interoperability</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-6"><li class="nav-item"><a class="nav-link" href="/interop/c-interop"><div><span>C interop</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/objective-c-interop"><div><span>Objective-C & Swift interop</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/java-interop"><div><span>Java & Kotlin interop</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-6-4" role="button" aria-expanded="false" aria-controls="-sidenav-6-4"><span>JavaScript interop</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-6-4"><li class="nav-item"><a class="nav-link" href="/interop/js-interop"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/usage"><div><span>Usage</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/js-types"><div><span>JS types</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/tutorials"><div><span>Tutorials</span></div></a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/past-js-interop"><div><span>Past JS interop</span></div></a></li><div class="sidenav-divider"></div><li class="nav-item"><a class="nav-link" href="/interop/js-interop/package-web"><div><span>Web interop</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link active collapsible" data-toggle="collapse" data-target="#-sidenav-7" role="button" aria-expanded="true" aria-controls="-sidenav-7"><span>Tools & techniques</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-7"><li class="nav-item"><a class="nav-link" href="/tools"><div><span>Overview</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-2" role="button" aria-expanded="false" aria-controls="-sidenav-7-2"><span>Editors & debuggers</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-2"><li class="nav-item"><a class="nav-link" href="/tools/jetbrains-plugin"><div><span>IntelliJ & Android Studio</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/vs-code"><div><span>VS Code</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-devtools"><div><span>Dart DevTools</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-2-4" role="button" aria-expanded="false" aria-controls="-sidenav-7-2-4"><span>DartPad</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-2-4"><li class="nav-item"><a class="nav-link" href="/tools/dartpad"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dartpad/troubleshoot"><div><span>Troubleshooting DartPad</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-3" role="button" aria-expanded="false" aria-controls="-sidenav-7-3"><span>Command-line tools</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-3"><li class="nav-item"><button class="nav-link collapsible" data-toggle="collapse" data-target="#-sidenav-7-3-1" role="button" aria-expanded="true" aria-controls="-sidenav-7-3-1"><span>Dart SDK</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-7-3-1"><li class="nav-item"><a class="nav-link" href="/tools/sdk"><div><span>Overview</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-tool"><div><span>dart</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-analyze"><div><span>dart analyze</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-compile"><div><span>dart compile</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-create"><div><span>dart create</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-doc"><div><span>dart doc</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-fix"><div><span>dart fix</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-format"><div><span>dart format</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-info"><div><span>dart info</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/cmd"><div><span>dart pub</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-run"><div><span>dart run</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-test"><div><span>dart test</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/dartaotruntime"><div><span>dartaotruntime</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/experiment-flags"><div><span>Experiment flags</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible" data-toggle="collapse" data-target="#-sidenav-7-3-2" role="button" aria-expanded="true" aria-controls="-sidenav-7-3-2"><span>Other command-line tools</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-7-3-2"><li class="nav-item"><a class="nav-link" href="/tools/build_runner"><div><span>build_runner</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/webdev"><div><span>webdev</span></div></a></li></ul></li></ul></li><li class="nav-item"><button class="nav-link active collapsible" data-toggle="collapse" data-target="#-sidenav-7-4" role="button" aria-expanded="true" aria-controls="-sidenav-7-4"><span>Static analysis</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-7-4"><li class="nav-item"><a class="nav-link active" href="/tools/analysis"><div><span>Customizing static analysis</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/non-promotion-reasons"><div><span>Fixing type promotion failures</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/linter-rules"><div><span>Linter rules</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tools/diagnostic-messages"><div><span>Diagnostic messages</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-7-5" role="button" aria-expanded="false" aria-controls="-sidenav-7-5"><span>Testing & optimization</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-7-5"><li class="nav-item"><a class="nav-link" href="/tools/testing"><div><span>Testing</span></div></a></li><li class="nav-item"><a class="nav-link" href="/web/debugging"><div><span>Debugging web apps</span></div></a></li></ul></li></ul></li><li aria-hidden="true"><div class="sidenav-divider"></div></li><li class="nav-item"><button class="nav-link collapsed collapsible" data-toggle="collapse" data-target="#-sidenav-9" role="button" aria-expanded="false" aria-controls="-sidenav-9"><span>Resources</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-9"><li class="nav-item"><a class="nav-link" href="/resources/dart-cheatsheet"><div><span>Language cheatsheet</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/breaking-changes"><div><span>Breaking changes</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/language/evolution"><div><span>Language evolution</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/language/spec"><div><span>Language specification</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/dart-3-migration"><div><span>Dart 3 migration guide</span></div></a></li><li class="nav-item"><button class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#-sidenav-9-6" role="button" aria-expanded="false" aria-controls="-sidenav-9-6"><span>Coming from ...</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse" id="-sidenav-9-6"><li class="nav-item"><a class="nav-link" href="/resources/coming-from/js-to-dart"><div><span>JavaScript to Dart</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/coming-from/swift-to-dart"><div><span>Swift to Dart</span></div></a></li></ul></li><div class="sidenav-divider"></div><li class="nav-item"><a class="nav-link" href="/resources/faq"><div><span>FAQ</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/glossary"><div><span>Glossary</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/books"><div><span>Books</span></div></a></li><li class="nav-item"><a class="nav-link" href="/resources/videos"><div><span>Videos</span></div></a></li><li class="nav-item"><a class="nav-link" href="/tutorials"><div><span>Tutorials</span></div></a></li></ul></li><li class="nav-item"><button class="nav-link collapsible" data-toggle="collapse" data-target="#-sidenav-10" role="button" aria-expanded="true" aria-controls="-sidenav-10"><span>Related sites</span> <span class="material-symbols expander" aria-hidden="true">expand_more</span></button><ul class="nav collapse show" id="-sidenav-10"><li class="nav-item"><a class="nav-link" href="https://api.dart.dev" target="_blank" rel="noopener"><div><span>API reference</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://medium.com/dartlang" target="_blank" rel="noopener"><div><span>Blog</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://dartpad.dev" target="_blank" rel="noopener"><div><span>DartPad (online editor)</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://flutter.dev" target="_blank" rel="noopener"><div><span>Flutter</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li><li class="nav-item"><a class="nav-link" href="https://pub.dev" target="_blank" rel="noopener"><div><span>Package site</span><span class="material-symbols" aria-hidden="true">open_in_new</span></div></a></li></ul></li></ul></div><main id="page-content"><div id="site-toc--side" class="site-toc"><header class="site-toc__title">Contents</header><ul class="section-nav"><li class="toc-entry nav-item"><a class="nav-link" href="#the-analysis-options-file">The analysis options file</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#enabling-additional-type-checks">Enabling stricter type checks</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#enabling-linter-rules">Enabling and disabling linter rules</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#lints">Enabling Dart team recommended linter rules</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#individual-rules">Enabling individual rules</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#disabling-individual-rules">Disabling individual rules</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#including-shared-options">Including shared options</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#plugins">Enabling analyzer plugins (experimental)</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#excluding-code-from-analysis">Excluding code from analysis</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#excluding-files">Excluding files</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#suppressing-diagnostics-for-a-file">Suppressing diagnostics for a file</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#suppressing-diagnostics-for-a-line-of-code">Suppressing diagnostics for a line of code</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#suppressing-diagnostics-in-a-pubspec-file">Suppressing diagnostics in a pubspec file</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#customizing-analysis-rules">Customizing analysis rules</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#ignoring-rules">Ignoring rules</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#changing-the-severity-of-rules">Changing the severity of rules</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#configuring-dart-format">Configuring dart format</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#resources">Resources</a></li></ul></div><article><div class="content"><div id="site-content-title"><h1>Customizing static analysis</h1></div><div id="site-toc--inline" class="site-toc toc-collapsible toc-collapsed"><header class="site-toc__title">Contents <span class="site-toc--inline__toggle toc-toggle-down"><i class="material-symbols">keyboard_arrow_down</i></span> <span class="site-toc--inline__toggle toc-toggle-up"><i class="material-symbols">keyboard_arrow_up</i></span></header><ul class="section-nav"><li class="toc-entry"><a href="#the-analysis-options-file">The analysis options file</a></li><li class="toc-entry"><a href="#enabling-additional-type-checks">Enabling stricter type checks</a></li><li class="toc-entry"><a href="#enabling-linter-rules">Enabling and disabling linter rules</a><ul><li class="toc-entry"><a href="#lints">Enabling Dart team recommended linter rules</a></li><li class="toc-entry"><a href="#individual-rules">Enabling individual rules</a></li><li class="toc-entry"><a href="#disabling-individual-rules">Disabling individual rules</a></li></ul></li><li class="toc-entry"><a href="#including-shared-options">Including shared options</a></li><li class="toc-entry"><a href="#plugins">Enabling analyzer plugins (experimental)</a></li><li class="toc-entry"><a href="#excluding-code-from-analysis">Excluding code from analysis</a><ul><li class="toc-entry"><a href="#excluding-files">Excluding files</a></li><li class="toc-entry"><a href="#suppressing-diagnostics-for-a-file">Suppressing diagnostics for a file</a></li><li class="toc-entry"><a href="#suppressing-diagnostics-for-a-line-of-code">Suppressing diagnostics for a line of code</a></li><li class="toc-entry"><a href="#suppressing-diagnostics-in-a-pubspec-file">Suppressing diagnostics in a pubspec file</a></li></ul></li><li class="toc-entry"><a href="#customizing-analysis-rules">Customizing analysis rules</a><ul><li class="toc-entry"><a href="#ignoring-rules">Ignoring rules</a></li><li class="toc-entry"><a href="#changing-the-severity-of-rules">Changing the severity of rules</a></li></ul></li><li class="toc-entry"><a href="#configuring-dart-format">Configuring dart format</a></li><li class="toc-entry"><a href="#resources">Resources</a></li></ul><span class="site-toc--inline__toggle toc-toggle-more-items"><i class="material-symbols">more_horiz</i></span></div> <?code-excerpt replace="/ *\/\/\s+ignore_for_file:[^\n]+\n//g; /(^|\n) *\/\/\s+ignore: (stable|beta|dev)[^\n]+\n/$1/g; /(\n[^\n]+) *\/\/\s+ignore: (stable|beta|dev)[^\n]+\n/$1\n/g; /. • (lib|test)\/\w+\.dart:\d+:\d+//g"?> <p>Static analysis allows you to find problems before executing a single line of code. It's a powerful tool used to prevent bugs and ensure that code conforms to style guidelines.</p><p>With the help of the analyzer, you can find simple typos. For example, perhaps an accidental semicolon made its way into an <code>if</code> statement:</p><blockquote class="ml-3"> <?code-excerpt "analysis/lib/lint.dart (empty_statements)" replace="/(if .*?)(;)/$1[!$2!]/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light show-line-numbers" tabindex="0"><code><span class="line" data-line="8"><span style="color:#D43324">void</span><span style="color:#6200EE"> increment</span><span style="color:#222222">() {</span></span> <span class="line" data-line="9"><span style="color:#D43324"> if</span><span style="color:#222222"> (count &#x3C; </span><span style="color:#11796D">10</span><span style="color:#222222">) </span><mark class="highlight"><span style="color:#222222">;</span></mark></span> <span class="line" data-line="10"><span style="color:#222222"> count++;</span></span> <span class="line" data-line="11"><span style="color:#222222">}</span></span></code></pre></div></div><p>If properly configured, the analyzer points to the semicolon and produces the following warning:</p> <?code-excerpt "analysis/analyzer-results-stable.txt" retain="empty_statements" replace="/lib\/lint.dart/example.dart/g"?> <div class="code-block-wrapper language-plaintext"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span>info - example.dart:9:19 - Unnecessary empty statement. Try removing the empty statement or restructuring the code. - empty_statements</span></span></code></pre></div></div></blockquote><p>The analyzer can also help you find more subtle problems. For example, perhaps you've forgotten to close a sink method:</p><blockquote class="ml-3"> <?code-excerpt "analysis/lib/lint.dart (close_sinks)" replace="/(contr.*?)(;)/[!$1!]$2/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> </span><mark class="highlight"><span style="color:#222222">controller = </span><span style="color:#0468D7">StreamController</span><span style="color:#222222">&#x3C;</span><span style="color:#0468D7">String</span><span style="color:#222222">>()</span></mark><span style="color:#222222">;</span></span></code></pre></div></div><?code-excerpt "analysis/analyzer-results-stable.txt" retain="close_sinks" replace="/-(.*?):(.*?):(.*?)-/-/g"?> <div class="code-block-wrapper language-plaintext"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span>info - Unclosed instance of 'Sink'. Try invoking 'close' in the function in which the 'Sink' was created. - close_sinks</span></span></code></pre></div></div></blockquote><p>In the Dart ecosystem, the Dart Analysis Server and other tools use the <a href="https://pub.dev/packages/analyzer">analyzer package</a> to perform static analysis.</p><p>You can customize static analysis to look for a variety of potential problems, including errors and warnings specified in the <a href="/resources/language/spec">Dart language spec</a>. You can also configure linter rules, to ensure that your code complies with the <a href="/effective-dart/style">Dart Style Guide</a> and other suggested guidelines in <a href="/effective-dart">Effective Dart</a>. Tools such as <a href="/tools/dart-analyze"><code>dart analyze</code></a>, <a href="https://docs.flutter.dev/testing/debugging#the-dart-analyzer"><code>flutter analyze</code></a>, and <a href="/tools#editors">IDEs and editors</a> use the analyzer package to evaluate your code.</p><p>This document explains how to customize the behavior of the analyzer using either an analysis options file or comments in Dart source code. If you want to add static analysis to your tool, see the <a href="https://pub.dev/packages/analyzer">analyzer package</a> docs and the <a href="https://htmlpreview.github.io/?https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/doc/api.html">Analysis Server API Specification.</a></p><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">info</span> <span>Note</span></div><div class="alert-content"><p>To view various analyzer diagnostics with explanations and common fixes, see <a href="/tools/diagnostic-messages">Diagnostic messages</a>.</p></div></aside><div class="header-wrapper"><h2 id="the-analysis-options-file">The analysis options file</h2><a class="heading-link" href="#the-analysis-options-file" aria-label="Link to 'The analysis options file' section">#</a></div><p>Place the analysis options file, <code>analysis_options.yaml</code>, at the root of the package, in the same directory as the pubspec file.</p><p>Here's a sample analysis options file:</p> <?code-excerpt "analysis_options.yaml" from="include" remove="implicit-dynamic" retain="/^$|\w+:|- cancel/" remove="https:"?> <div class="code-block-wrapper language-yaml"><div class="code-block-header">analysis_options.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">: </span><span style="color:#11796D">package:lints/recommended.yaml</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> exclude</span><span style="color:#222222">: [</span><span style="color:#11796D">build/**</span><span style="color:#222222">]</span></span> <span class="line"><span style="color:#0468D7"> language</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> strict-casts</span><span style="color:#222222">: </span><span style="color:#11796D">true</span></span> <span class="line"><span style="color:#0468D7"> strict-raw-types</span><span style="color:#222222">: </span><span style="color:#11796D">true</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7">linter</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> rules</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">cancel_subscriptions</span></span></code></pre></div></div><p>The sample illustrates the most common top-level entries:</p><ul><li>Use <code>include: <em>url</em></code> to bring in options from the specified URL—in this case, from a file in the <code>lints</code> package. Because YAML doesn't allow duplicate keys, you can include at most one file.</li><li>Use the <code>analyzer:</code> entry to customize static analysis: <a href="#enabling-additional-type-checks">enabling stricter type checks</a>, <a href="#excluding-files">excluding files</a>, <a href="#ignoring-rules">ignoring specific rules</a>, <a href="#changing-the-severity-of-rules">changing the severity of rules</a>, or <a href="/tools/experiment-flags#using-experiment-flags-with-the-dart-analyzer-command-line-and-ide">enabling experiments</a>.</li><li>Use the <code>linter:</code> entry to configure <a href="#enabling-linter-rules">linter rules</a>.</li></ul><aside class="alert alert-warning"><div class="alert-header"><span class="material-symbols" aria-hidden="true">warning</span> <span>Warning</span></div><div class="alert-content"><p><strong>YAML is sensitive to whitespace.</strong> Don't use tabs in a YAML file, and use 2 spaces to denote each level of indentation.</p></div></aside><p>If the analyzer can't find an analysis options file at the package root, it walks up the directory tree, looking for one. If no file is available, the analyzer defaults to standard checks.</p><p>Consider the following directory structure for a large project:</p><p><img src="/assets/img/guides/analysis-options-directory-structure.png" alt="project root contains analysis_options.yaml (#1) and 3 packages, one of which (my_package) contains an analysis_options.yaml file (#2)."></p><p>The analyzer uses file #1 to analyze the code in <code>my_other_package</code> and <code>my_other_other_package</code>, and file #2 to analyze the code in <code>my_package</code>.</p><div class="header-wrapper"><h2 id="enabling-additional-type-checks">Enabling stricter type checks</h2><a class="heading-link" href="#enabling-additional-type-checks" aria-label="Link to 'Enabling stricter type checks' section">#</a></div><p>If you want stricter static checks than the <a href="/language/type-system">Dart type system</a> requires, consider enabling the <code>strict-casts</code>, <code>strict-inference</code>, and <code>strict-raw-types</code> language modes:</p> <?code-excerpt "analysis/analysis_options.yaml" from="analyzer" to="strict-raw-types" remove="exclude"?> <div class="code-block-wrapper language-yaml"><div class="code-block-header">analysis_options.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> language</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> strict-casts</span><span style="color:#222222">: </span><span style="color:#11796D">true</span></span> <span class="line"><span style="color:#0468D7"> strict-inference</span><span style="color:#222222">: </span><span style="color:#11796D">true</span></span> <span class="line"><span style="color:#0468D7"> strict-raw-types</span><span style="color:#222222">: </span><span style="color:#11796D">true</span></span></code></pre></div></div><p>You can use the modes together or separately; all default to <code>false</code>.</p><dl><dt><code>strict-casts: &lt;bool&gt;</code></dt><dd>A value of <code>true</code> ensures that the type inference engine never implicitly casts from <code>dynamic</code> to a more specific type. The following valid Dart code includes an implicit downcast from the <code>dynamic</code> value returned by <code>jsonDecode</code> to <code>List&lt;String&gt;</code> that could fail at runtime. This mode reports the potential error, requiring you to add an explicit cast or otherwise adjust your code.</dd></dl> <?code-excerpt "analysis/lib/strict_modes.dart (strict-casts)" replace="/jsonDecode\(jsonText\)/[!$&!]/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body has-tag tag-fails-sa"><span class="code-block-tag">✗ static analysis: failure</span><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> foo</span><span style="color:#222222">(</span><span style="color:#0468D7">List</span><span style="color:#222222">&#x3C;</span><span style="color:#0468D7">String</span><span style="color:#222222">> lines) {</span></span> <span class="line"><span style="color:#222222"> ...</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> bar</span><span style="color:#222222">(</span><span style="color:#0468D7">String</span><span style="color:#222222"> jsonText) {</span></span> <span class="line"><span style="color:#6200EE"> foo</span><span style="color:#222222">(</span><mark class="highlight"><span style="color:#6200EE">jsonDecode</span><span style="color:#222222">(jsonText)</span></mark><span style="color:#222222">); </span><span style="color:#6E6E70">// Implicit cast</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><?code-excerpt "analysis/analyzer-results-stable.txt" retain="The argument type 'dynamic' can't be assigned" replace="/-(.*?):(.*?):(.*?)-/-/g"?> <div class="code-block-wrapper language-plaintext"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span>error - The argument type 'dynamic' can't be assigned to the parameter type 'List&#x3C;String>'. - argument_type_not_assignable</span></span></code></pre></div></div><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">merge_type</span> <span>Version note</span></div><div class="alert-content"><p>The <code>strict-casts</code> mode was introduced in Dart 2.16. To enable similar checks with earlier SDK releases, consider using the now deprecated <code>implicit-casts</code> option:</p><div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> strong-mode</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> implicit-casts</span><span style="color:#222222">: </span><span style="color:#11796D">false</span></span></code></pre></div></div></div></aside><dl><dt><code>strict-inference: &lt;bool&gt;</code></dt><dd>A value of <code>true</code> ensures that the type inference engine never chooses the <code>dynamic</code> type when it can't determine a static type. The following valid Dart code creates a <code>Map</code> whose type argument cannot be inferred, resulting in an inference failure hint by this mode:</dd></dl> <?code-excerpt "analysis/lib/strict_modes.dart (strict-inference)" replace="/{}/[!$&!]/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body has-tag tag-fails-sa"><span class="code-block-tag">✗ static analysis: failure</span><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> lines = </span><mark class="highlight"><span style="color:#222222">{}</span></mark><span style="color:#222222">; </span><span style="color:#6E6E70">// Inference failure</span></span> <span class="line"><span style="color:#222222">lines[</span><span style="color:#11796D">'Dart'</span><span style="color:#222222">] = </span><span style="color:#11796D">10000</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">lines[</span><span style="color:#11796D">'C++'</span><span style="color:#222222">] = </span><span style="color:#11796D">'one thousand'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">lines[</span><span style="color:#11796D">'Go'</span><span style="color:#222222">] = </span><span style="color:#11796D">2000</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#11796D">'Lines: </span><span style="color:#11796D">${</span><span style="color:#222222">lines</span><span style="color:#11796D">.</span><span style="color:#222222">values</span><span style="color:#11796D">.</span><span style="color:#6200EE">reduce</span><span style="color:#11796D">((</span><span style="color:#222222">a</span><span style="color:#11796D">, </span><span style="color:#222222">b</span><span style="color:#11796D">) => </span><span style="color:#222222">a</span><span style="color:#11796D"> + </span><span style="color:#222222">b</span><span style="color:#11796D">)}</span><span style="color:#11796D">'</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Runtime error</span></span></code></pre></div></div><p></p> <?code-excerpt "analysis/analyzer-results-stable.txt" retain="The type argument(s) of 'Map'" replace="/. Use.*'Map'. / /g; /-(.*?):(.*?):(.*?)-/-/g"?> <div class="code-block-wrapper language-plaintext"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span>warning - The type argument(s) of 'Map' can't be inferred - inference_failure_on_collection_literal</span></span></code></pre></div></div><aside class="alert alert-success"><div class="alert-header"><span class="material-symbols" aria-hidden="true">lightbulb</span> <span>Tip</span></div><div class="alert-content"><p>The <code>strict-inference</code> mode can identify many situations which result in an inference failure.</p><p>See <a href="https://github.com/dart-lang/language/blob/main/resources/type-system/strict-inference.md#conditions-for-strict-inference-failure">Conditions for strict inference failure</a> for an exhaustive list of inference failure conditions.</p></div></aside><dl><dt><code>strict-raw-types: &lt;bool&gt;</code></dt><dd>A value of <code>true</code> ensures that the type inference engine never chooses the <code>dynamic</code> type when it can't determine a static type due to omitted type arguments. The following valid Dart code has a <code>List</code> variable with a raw type, resulting in a raw type hint by this mode:</dd></dl> <?code-excerpt "analysis/lib/strict_modes.dart (strict-raw-types)" replace="/List n/[!List!] n/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body has-tag tag-fails-sa"><span class="code-block-tag">✗ static analysis: failure</span><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><mark class="highlight"><span style="color:#0468D7">List</span></mark><span style="color:#222222"> numbers = [</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">]; </span><span style="color:#6E6E70">// List with raw type</span></span> <span class="line"><span style="color:#D43324">for</span><span style="color:#222222"> (</span><span style="color:#D43324">final</span><span style="color:#222222"> n </span><span style="color:#D43324">in</span><span style="color:#222222"> numbers) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(n.length); </span><span style="color:#6E6E70">// Runtime error</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><?code-excerpt "analysis/analyzer-results-stable.txt" retain="The generic type" replace="/. Use explicit.*\. / /g; /-(.*?):(.*?):(.*?)-/-/g"?> <div class="code-block-wrapper language-plaintext"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span>warning - The generic type 'List&#x3C;dynamic>' should have explicit type arguments but doesn't - strict_raw_type</span></span></code></pre></div></div><div class="header-wrapper"><h2 id="enabling-linter-rules">Enabling and disabling linter rules</h2><a class="heading-link" href="#enabling-linter-rules" aria-label="Link to 'Enabling and disabling linter rules' section">#</a></div><p>The analyzer package also provides a code linter. A wide variety of <a href="/tools/linter-rules">linter rules</a> are available. Linters tend to be nondenominational—rules don't have to agree with each other. For example, some rules are more appropriate for regular Dart packages and others are designed for Flutter apps. Note that linter rules can have false positives, unlike static analysis.</p><div class="header-wrapper"><h3 id="lints">Enabling Dart team recommended linter rules</h3><a class="heading-link" href="#lints" aria-label="Link to 'Enabling Dart team recommended linter rules' section">#</a></div><p>The Dart team provides two sets of recommended linter rules in the <a href="https://pub.dev/packages/lints">lints package</a>:</p><dl><dt>Core rules</dt><dd>Help identify critical issues that are likely to lead to problems when running or consuming Dart code. All code should pass these linter rules. Packages that are uploaded to <a href="https://pub.dev">pub.dev</a> have a <a href="https://pub.dev/help/scoring">package score</a> that's based in part on passing these rules.</dd><dt>Recommended rules</dt><dd>Help identify additional issues that may lead to problems when running or consuming Dart code, and enforce a single, idiomatic style and format. We recommend that all Dart code use these rules, which are a superset of the core rules.</dd></dl><aside class="alert alert-success"><div class="alert-header"><span class="material-symbols" aria-hidden="true">lightbulb</span> <span>Tip</span></div><div class="alert-content"><p>If you're working on Flutter code, then instead of using the <code>lints</code> package, use <a href="https://pub.dev/packages/flutter_lints"><code>flutter_lints</code></a>, which provides a superset of the recommended rules.</p></div></aside><p>To enable either set of lints, add the <a href="https://pub.dev/packages/lints">lints package</a> as a dev dependency:</p><div class="code-block-wrapper language-console"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#222222">$ dart pub add --dev lints</span></span></code></pre></div></div><p>Then edit your <code>analysis_options.yaml</code> file to include your preferred rule set:</p><div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">: </span><span style="color:#11796D">package:lints/&#x3C;RULE_SET>.yaml</span></span></code></pre></div></div><p>For example, you can include the recommended rule set like this:</p><div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">: </span><span style="color:#11796D">package:lints/recommended.yaml</span></span></code></pre></div></div><aside class="alert alert-important"><div class="alert-header"><span class="material-symbols" aria-hidden="true">feedback</span> <span>Important</span></div><div class="alert-content"><p>When a <strong>new version of <code>lints</code></strong> is published, code that previously passed analysis might <strong>start failing analysis.</strong> We recommend updating your code to work with the new rules. Other options are to explicitly enable individual linter rules or <a href="#disabling-individual-rules">disable individual rules</a>.</p></div></aside><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">info</span> <span>Note</span></div><div class="alert-content"><p>For more information about including options files, check out the <a href="#including-shared-options">Including shared options</a> section.</p></div></aside><div class="header-wrapper"><h3 id="individual-rules">Enabling individual rules</h3><a class="heading-link" href="#individual-rules" aria-label="Link to 'Enabling individual rules' section">#</a></div><p>To enable a single linter rule, add <code>linter:</code> to the analysis options file as a top-level key, followed by <code>rules:</code> as a second-level key. On subsequent lines, specify the rules that you want to apply, prefixed with dashes (the syntax for a YAML list). For example:</p> <?code-excerpt "analysis_options.yaml" from="linter:" take="12" remove="https:"?> <div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">linter</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> rules</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">always_declare_return_types</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">cancel_subscriptions</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">close_sinks</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">combinators_ordering</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">comment_references</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">invalid_case_patterns</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">one_member_abstracts</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">only_throw_errors</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">prefer_single_quotes</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="disabling-individual-rules">Disabling individual rules</h3><a class="heading-link" href="#disabling-individual-rules" aria-label="Link to 'Disabling individual rules' section">#</a></div><p>If you include an analysis options file such as the one in <code>lints</code>, you might want to disable some of the included rules. Disabling individual rules is similar to enabling them, but requires the use of a map rather than a list as the value for the <code>rules:</code> entry, so each line should contain the name of a rule followed by either <code>: false</code> or <code>: true</code>.</p><p>Here's an example of an analysis options file that uses all the recommended rules from <code>lints</code> except <code>avoid_shadowing_type_parameters</code>. It also enables the lint <code>await_only_futures</code>:</p> <?code-excerpt "analysis_alt/analysis_options_linter.yaml"?> <div class="code-block-wrapper language-yaml"><div class="code-block-header">analysis_options.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">: </span><span style="color:#11796D">package:lints/recommended.yaml</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7">linter</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> rules</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> avoid_shadowing_type_parameters</span><span style="color:#222222">: </span><span style="color:#11796D">false</span></span> <span class="line"><span style="color:#0468D7"> await_only_futures</span><span style="color:#222222">: </span><span style="color:#11796D">true</span></span></code></pre></div></div><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">info</span> <span>Note</span></div><div class="alert-content"><p>Due to YAML restrictions, <strong>you can't mix list and key-value syntax in the same <code>rules</code> entry.</strong> You can use the other syntax for rules in an included file.</p></div></aside><div class="header-wrapper"><h2 id="including-shared-options">Including shared options</h2><a class="heading-link" href="#including-shared-options" aria-label="Link to 'Including shared options' section">#</a></div><p>An analysis options file can include options which are specified in another options file, or even a list of other options files. You can specify such files using the top-level <code>include:</code> field:</p><div class="code-block-wrapper language-yaml"><div class="code-block-header">analysis_options.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">: </span><span style="color:#11796D">package:flutter_lints/recommended.yaml</span></span></code></pre></div></div><p>An included options file can be specified with a <code>package:</code> path, or a relative path. Multiple analysis options files can be specified in a list:</p><div class="code-block-wrapper language-yaml"><div class="code-block-header">analysis_options.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">package:flutter_lints/recommended.yaml</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">../team_options.yaml</span></span></code></pre></div></div><p>Options in an included file can be overridden in the including file, as well as by subsequent included files. In other words, the options specified by an analysis options file are computed by first applying the options specified in each of the included files (by recursively applying this algorithm), in the order they appear in the list, and then overriding them with any locally defined options.</p><p>For example, given the following options files:</p><div class="code-block-wrapper language-yaml"><div class="code-block-header">three.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">: </span><span style="color:#11796D">two.yaml</span></span> <span class="line"><span style="color:#6E6E70"># ...</span></span></code></pre></div></div><p>And a final options file that includes these:</p><div class="code-block-wrapper language-yaml"><div class="code-block-header">analysis_options.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">include</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">one.yaml</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">three.yaml</span></span> <span class="line"><span style="color:#6E6E70"># ...</span></span></code></pre></div></div><p>Then the combined analysis options are computed by applying the options found in <code>one.yaml</code>, then <code>two.yaml</code>, then <code>three.yaml</code>, and finally <code>analysis_options.yaml</code>.</p><div class="header-wrapper"><h2 id="plugins">Enabling analyzer plugins (experimental)</h2><a class="heading-link" href="#plugins" aria-label="Link to 'Enabling analyzer plugins (experimental)' section">#</a></div><p>The analyzer has experimental support for plugins. These plugins integrate with the analyzer to add functionality such as new diagnostics, quick fixes, and custom code completion. You can enable only one plugin per <code>analysis_options.yaml</code> file. Enabling an analyzer plugin increases how much memory the analyzer uses.</p><p>Don't use analyzer plugins if your situation meets either of the following conditions:</p><ul><li>You use a development machine with less than 16 GB of memory.</li><li>You use a mono-repo with more than 10 <code>pubspec.yaml</code> and <code>analysis_options.yaml</code> files.</li></ul><p>You can find a few analyzer plugins on <a href="https://pub.dev/packages?q=dependency%3Aanalyzer_plugin">pub.dev</a>.</p><p>To enable a plugin:</p><ol><li><p>Add the package containing the plugin as a dev dependency.</p><div class="code-block-wrapper language-console"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#222222">$ dart pub add --dev &#x3C;your_favorite_analyzer_plugin_package></span></span></code></pre></div></div></li><li><p>Edit your <code>analysis_options.yaml</code> file to enable the plugin.</p><div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> plugins</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">your_favorite_analyzer_plugin_package</span></span></code></pre></div></div><p>To indicate specific plugin functionality to enable, such as new diagnostics, additional setup might be required.</p></li></ol><div class="header-wrapper"><h2 id="excluding-code-from-analysis">Excluding code from analysis</h2><a class="heading-link" href="#excluding-code-from-analysis" aria-label="Link to 'Excluding code from analysis' section">#</a></div><p>Sometimes it's OK for some code to fail analysis. For example, you might rely on code generated by a package that you don't own—the generated code works, but produces warnings during static analysis. Or a linter rule might cause a false positive that you want to suppress.</p><p>You have a few ways to exclude code from analysis:</p><ul><li>Exclude entire files from analysis.</li><li>Stop specific non-error rules from being applied to individual files.</li><li>Stop specific non-error rules from being applied to individual lines of code.</li></ul><p>You can also <a href="#disabling-individual-rules">disable specific rules</a> for all files or <a href="#changing-the-severity-of-rules">change the severity of rules</a>.</p><div class="header-wrapper"><h3 id="excluding-files">Excluding files</h3><a class="heading-link" href="#excluding-files" aria-label="Link to 'Excluding files' section">#</a></div><p>To exclude files from static analysis, use the <code>exclude:</code> analyzer option. You can list individual files, or use <a href="https://pub.dev/packages/glob">glob</a> pattern syntax. All usages of glob patterns should be relative to the directory containing the <code>analysis_options.yaml</code> file.</p> <?code-excerpt "analysis_alt/analysis_options.yaml (exclude)" plaster="none"?> <div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> exclude</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">lib/client.dart</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">lib/server/*.g.dart</span></span> <span class="line"><span style="color:#222222"> - </span><span style="color:#11796D">test/_data/**</span></span></code></pre></div></div><p><a id="suppressing-rules-for-a-file"></a></p><div class="header-wrapper"><h3 id="suppressing-diagnostics-for-a-file">Suppressing diagnostics for a file</h3><a class="heading-link" href="#suppressing-diagnostics-for-a-file" aria-label="Link to 'Suppressing diagnostics for a file' section">#</a></div><p>To ignore a specific non-error diagnostic for a specific file, add an <code>ignore_for_file</code> comment to the file:</p> <?code-excerpt "analysis/lib/assignment.dart (ignore_for_file)" replace="/, \w+//g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// ignore_for_file: unused_local_variable</span></span></code></pre></div></div><p>This acts for the whole file, before or after the comment, and is particularly useful for generated code.</p><p>To suppress more than one diagnostic, use a comma-separated list:</p> <?code-excerpt "analysis/lib/assignment.dart (ignore_for_file)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// ignore_for_file: unused_local_variable, duplicate_ignore, dead_code</span></span></code></pre></div></div><p>To suppress all linter rules, add a <code>type=lint</code> specifier:</p> <?code-excerpt "analysis/lib/ignore_lints.dart (ignore_type_for_file)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// ignore_for_file: type=lint</span></span></code></pre></div></div><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">merge_type</span> <span>Version note</span></div><div class="alert-content"><p>Support for the <code>type=lint</code> specifier was added in Dart 2.15.</p></div></aside><p><a id="suppressing-rules-for-a-line-of-code"></a></p><div class="header-wrapper"><h3 id="suppressing-diagnostics-for-a-line-of-code">Suppressing diagnostics for a line of code</h3><a class="heading-link" href="#suppressing-diagnostics-for-a-line-of-code" aria-label="Link to 'Suppressing diagnostics for a line of code' section">#</a></div><p>To suppress a specific non-error diagnostic on a specific line of Dart code, put an <code>ignore</code> comment above the line of code. Here's an example of ignoring code that causes a runtime error, as you might do in a language test:</p> <?code-excerpt "analysis/lib/assignment.dart (invalid_assignment)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// ignore: invalid_assignment</span></span> <span class="line"><span style="color:#0468D7">int</span><span style="color:#222222"> x = </span><span style="color:#11796D">''</span><span style="color:#222222">;</span></span></code></pre></div></div><p>To suppress more than one diagnostic, supply a comma-separated list:</p> <?code-excerpt "analysis/lib/assignment.dart (ignore-more)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// ignore: invalid_assignment, const_initialized_with_non_constant_value</span></span> <span class="line"><span style="color:#D43324">const</span><span style="color:#222222"> x = y;</span></span></code></pre></div></div><p>Alternatively, append the ignore comment to the line that it applies to:</p> <?code-excerpt "analysis/lib/assignment.dart (single-line)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">int</span><span style="color:#222222"> x = </span><span style="color:#11796D">''</span><span style="color:#222222">; </span><span style="color:#6E6E70">// ignore: invalid_assignment</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="suppressing-diagnostics-in-a-pubspec-file">Suppressing diagnostics in a pubspec file</h3><a class="heading-link" href="#suppressing-diagnostics-in-a-pubspec-file" aria-label="Link to 'Suppressing diagnostics in a pubspec file' section">#</a></div><p>If you need to suppress a non-error diagnostic from the analyzer in a <code>pubspec.yaml</code> file, add an <code>ignore</code> comment above the affected line.</p><p>The following example ignores the <a href="/tools/linter-rules/sort_pub_dependencies"><code>sort_pub_dependencies</code></a> lint as it wants to put the <code>flutter</code> dependency first:</p><div class="code-block-wrapper language-yaml"><div class="code-block-header">pubspec.yaml</div><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">dependencies</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> flutter</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> sdk</span><span style="color:#222222">: </span><span style="color:#11796D">flutter</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> # ignore: sort_pub_dependencies</span></span> <span class="line"><span style="color:#0468D7"> collection</span><span style="color:#222222">: </span><span style="color:#11796D">^1.19.0</span></span></code></pre></div></div><aside class="alert alert-info"><div class="alert-header"><span class="material-symbols" aria-hidden="true">merge_type</span> <span>Version note</span></div><div class="alert-content"><p>Support for ignore comments in <code>pubspec.yaml</code> files was added in Dart 3.3. If you are using Dart 3.2 or earlier, the ignored diagnostic will still be triggered.</p></div></aside><div class="header-wrapper"><h2 id="customizing-analysis-rules">Customizing analysis rules</h2><a class="heading-link" href="#customizing-analysis-rules" aria-label="Link to 'Customizing analysis rules' section">#</a></div><p>Each <a href="/tools/diagnostic-messages">analyzer diagnostic</a> and <a href="/tools/linter-rules">linter rule</a> has a default severity. You can use the analysis options file to change the severity of individual rules, or to always ignore some rules.</p><p>The analyzer supports three severity levels:</p><dl><dt><code>info</code></dt><dd>An informational message that doesn't cause analysis to fail. Example: <a href="/tools/diagnostic-messages#dead_code"><code>dead_code</code></a></dd><dt><code>warning</code></dt><dd>A warning that doesn't cause analysis to fail unless the analyzer is configured to treat warnings as errors. Example: <a href="/tools/diagnostic-messages#invalid_null_aware_operator"><code>invalid_null_aware_operator</code></a></dd><dt><code>error</code></dt><dd>An error that causes analysis to fail. Example: <a href="/tools/diagnostic-messages#invalid_assignment"><code>invalid_assignment</code></a></dd></dl><div class="header-wrapper"><h3 id="ignoring-rules">Ignoring rules</h3><a class="heading-link" href="#ignoring-rules" aria-label="Link to 'Ignoring rules' section">#</a></div><p>You can ignore specific <a href="/tools/diagnostic-messages">analyzer diagnostics</a> and <a href="/tools/linter-rules">linter rules</a> by using the <code>errors:</code> field. List the rule, followed by <code>: ignore</code>. For example, the following analysis options file instructs the analysis tools to ignore the TODO rule:</p> <?code-excerpt "analysis_alt/analysis_options.yaml (errors)" to="ignore"?> <div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> errors</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> todo</span><span style="color:#222222">: </span><span style="color:#11796D">ignore</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="changing-the-severity-of-rules">Changing the severity of rules</h3><a class="heading-link" href="#changing-the-severity-of-rules" aria-label="Link to 'Changing the severity of rules' section">#</a></div><p>You can globally change the severity of a particular rule. This technique works for regular analysis issues as well as for lints. For example, the following analysis options file instructs the analysis tools to treat invalid assignments as warnings and missing returns as errors, and to provide information (but not a warning or error) about dead code:</p> <?code-excerpt "analysis_alt/analysis_options.yaml (errors)" remove="ignore"?> <div class="code-block-wrapper language-yaml"><div class="code-block-body"><span class="code-block-language" title="Language yaml">yaml</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">analyzer</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> errors</span><span style="color:#222222">:</span></span> <span class="line"><span style="color:#0468D7"> invalid_assignment</span><span style="color:#222222">: </span><span style="color:#11796D">warning</span></span> <span class="line"><span style="color:#0468D7"> missing_return</span><span style="color:#222222">: </span><span style="color:#11796D">error</span></span> <span class="line"><span style="color:#0468D7"> dead_code</span><span style="color:#222222">: </span><span style="color:#11796D">info</span></span></code></pre></div></div><div class="header-wrapper"><h2 id="configuring-dart-format">Configuring <code>dart format</code></h2><a class="heading-link" href="#configuring-dart-format" aria-label="Link to 'Configuring dart format' section">#</a></div><p>You can configure the behavior of <a href="/tools/dart-format"><code>dart format</code></a> by adding a <code>formatter</code> section to the analysis options file specifying your preferred <code>page_width</code>.</p><p>For more information, read <a href="/tools/dart-format#configuring-formatter-page-width">Configuring formatter page width</a>.</p><div class="header-wrapper"><h2 id="resources">Resources</h2><a class="heading-link" href="#resources" aria-label="Link to 'Resources' section">#</a></div><p>Use the following resources to learn more about static analysis in Dart:</p><ul><li><a href="/language/type-system">Dart's type system</a></li><li><a href="/tools/linter-rules">Dart linter rules</a></li><li><a href="https://pub.dev/packages/analyzer">analyzer package</a></li></ul><p id="page-github-links"><span>Unless stated otherwise, the documentation on this site reflects Dart 3.7.1. Page last updated on 2025-02-12.</span> <a href="https://github.com/dart-lang/site-www/tree/main/src/content/tools/analysis.md" target="_blank" rel="noopener">View source</a> <span>or </span><a href="https://github.com/dart-lang/site-www/issues/new?template=1_page_issue.yml&page-url=https://dart.dev/tools/analysis/&page-source=https://github.com/dart-lang/site-www/tree/main/src/content/tools/analysis.md" title="Report an issue with this page" target="_blank" rel="noopener">report an issue</a>.</p></div></article></main></div><footer id="page-footer"><div class="footer-section footer-main"><a href="/" class="brand" title="Dart"><img src="/assets/img/logo/logo-white-text.svg" alt="Dart" width="164"></a><div class="footer-social-links"><a href="https://medium.com/dartlang" target="_blank" rel="noopener" title="Dart's Medium publication"><svg><use href="/assets/img/social/medium.svg#medium"></use></svg> </a><a href="https://github.com/dart-lang" target="_blank" rel="noopener" title="Dart's GitHub organization"><svg><use href="/assets/img/social/github.svg#github"></use></svg> </a><a href="https://bsky.app/profile/dart.dev" target="_blank" rel="noopener" title="Dart's Bluesky profile"><svg><use href="/assets/img/social/bluesky.svg#bluesky"></use></svg> </a><a href="https://twitter.com/dart_lang" target="_blank" rel="noopener" title="Dart's X (Twitter) profile"><svg><use href="/assets/img/social/x.svg#x"></use></svg></a></div></div><div class="footer-section footer-tray"><div class="footer-licenses">Except as otherwise noted, this site is licensed under a <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>, and code samples are licensed under the <a href="https://opensource.org/licenses/BSD-3-Clause">3-Clause BSD License</a>.</div><div class="footer-utility-links"><ul><li><a href="/terms" title="Terms of use">Terms</a></li><li><a href="https://policies.google.com/privacy" target="_blank" rel="noopener" title="Privacy policy">Privacy</a></li><li><a href="/security" title="Security philosophy and practices">Security</a></li></ul></div></div></footer></div></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10