---
title: Avoid duplicate case values in a switch
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Avoid duplicate case values in a switch
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Avoid duplicate case values in a switch

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `dart-code-quality/no-duplicate-case-values`

**Language:** Dart

**Severity:** Warning

**Category:** Code Style

## Description{% #description %}

When two case clauses in the same switch share the same constant value, only the first one is reachable and the later one is dead code, which is usually a copy-paste mistake. Remove the duplicate case or correct its value so each case is distinct and reachable.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```dart
void main(int x, String s, bool a) {
  switch (x) {
    case 1:
      print('one');
      break;
    case 2:
      print('two');
      break;
    case 1:
      print('duplicate one');
      break;
    case 2 when a:
      print('duplicate two');
      break;
  }

  switch (s) {
    case 'a':
      print('a');
      break;
    case 'a':
      print('duplicate a');
      break;
  }
}
```

## Compliant Code Examples{% #compliant-code-examples %}

```dart
void main(int x, String s, bool a, bool b) {
  switch (x) {
    case 1:
      print('one');
      break;
    case 2:
      print('two');
      break;
    default:
      print('other');
  }

  switch (s) {
    case 'a':
      print('a');
      break;
    case 'b':
      print('b');
      break;
  }

  switch (x) {
    case 1 when a:
      print('one-a');
      break;
    case 1 when b:
      print('one-b');
      break;
  }

  switch (x) {
    case 1:
      print('again');
      break;
  }

  // Guarded case before a plain case: the plain `case 1:` is still reachable
  // (it matches when the guard is false), so nothing is flagged.
  switch (x) {
    case 1 when a:
      print('one-a');
      break;
    case 1:
      print('one');
      break;
  }
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 