# W1003 unused_import

URL: /docs/errors/W1003
Markdown: /docs/errors/W1003.md

> An imported name is never used in this module: not in a value, a type, a pattern or a @contract_test attribute.

A warning from the `resolve` phase. `sakura explain W1003` prints this explanation offline.

An imported name is never used in this module: not in a value, a type, a pattern or a
`@contract_test` attribute. The fix removes the name, or the whole `import` when nothing it
brings is used, so `sakura fix` applies it (design §17.294 RQ-5).

Warning:

```sakura
import std/json.{DecodeError, Json}
fn show(n: Int) -> String { Json.encode(n) }
```

Fixed:

```sakura
import std/json.{Json}
fn show(n: Int) -> String { Json.encode(n) }
```

## Wrong program [#wrong-program]

[`compiler/tests/ui/W1003_unused_import.sak`](https://github.com/icantcodefyi/sakuralang/blob/main/compiler/tests/ui/W1003_unused_import.sak), which the compiler's ui suite runs through `sakura check`:

```sakura
// §17.294 RQ-5: one W1003 per unused imported name. `Json`, the last of its list, goes with the
// comma before it and `Level`, the first, with the comma after it; the `import`s of `retry`,
// `crypto` and `Violation` go whole. A name used in a value (`None`), only in a type
// (`DecodeError`), only in a pattern (`Some`) or only in a `@contract_test` attribute (`Log`)
// is used.
import std/core.{Violation}
import std/crypto
import std/json.{DecodeError, Json}
import std/log.{Level, Log}
import std/option.{None, Some}
import std/retry.{retry}

@contract_test(handlers: [Log.captured()])
pub fn show(n: Int) -> String
  requires n > 0
{ "{n}" }

pub fn same(e: DecodeError) -> DecodeError { e }

pub fn is_some(o: Option<Int>) -> Bool {
  match o {
    Some(_) => true,
    _ => false,
  }
}

pub fn nothing() -> Option<Int> { None }
```

`sakura check --json` reports this record (pretty-printed; `$DIR` is the fixture's directory):

```json
{
  "schema_version": "1.0",
  "kind": "diagnostic",
  "id": "d:68b46a6d7cb813e4",
  "code": "W1003",
  "severity": "warning",
  "phase": "resolve",
  "message": "`Violation` is imported but never used",
  "snapshot": "sha256:$SNAPSHOT",
  "symbol_id": null,
  "spans": [
    {
      "uri": "file://$DIR/W1003_unused_import.sak",
      "content_hash": "sha256:7cc3be26d97189e06f6d3c7ab675b81c0aae6d3882a6186b4c7277f555044e0f",
      "start_byte": 460,
      "end_byte": 469,
      "role": "primary"
    }
  ],
  "related_ids": [],
  "expected": null,
  "actual": null,
  "obligation": null,
  "counterexample": null,
  "explanation_uri": "sakura://explain/W1003",
  "fixes": [
    {
      "id": "f:68b46a6d7cb813e4.0",
      "title": "Remove the import",
      "applicability": "machine_applicable",
      "impact": "local",
      "edits": [
        {
          "span": {
            "uri": "file://$DIR/W1003_unused_import.sak",
            "content_hash": "sha256:7cc3be26d97189e06f6d3c7ab675b81c0aae6d3882a6186b4c7277f555044e0f",
            "start_byte": 443,
            "end_byte": 471,
            "role": "primary"
          },
          "replacement": ""
        }
      ]
    }
  ]
}
```

## Fixed program [#fixed-program]

[`compiler/tests/ui/W1003_unused_import.fixed.sak`](https://github.com/icantcodefyi/sakuralang/blob/main/compiler/tests/ui/W1003_unused_import.fixed.sak), which `sakura check` accepts with no diagnostics:

```sakura
import std/json.{DecodeError}
import std/log.{Log}
import std/option.{None, Some}

@contract_test(handlers: [Log.captured()])
pub fn show(n: Int) -> String
  requires n > 0
{ "{n}" }

pub fn same(e: DecodeError) -> DecodeError { e }

pub fn is_some(o: Option<Int>) -> Bool {
  match o {
    Some(_) => true,
    _ => false,
  }
}

pub fn nothing() -> Option<Int> { None }
```
