---
title: Unnecessary unsafe (UnnecessaryUnsafe)
framework: swift-compiler
role: article
role_heading: Article
path: swift-compiler/documentation/diagnostics/unnecessary-unsafe
---

# Unnecessary unsafe (UnnecessaryUnsafe)

## Overview

Overview This warning is emitted when an unsafe marker appears on an expression that contains no unsafe operations. The marker may have been added preemptively or the unsafe operation it was needed for may no longer be considered unsafe. This group is a subgroup of Unnecessary effect markers (UnnecessaryEffectMarker). Example Some APIs with ‘unsafe’ in their names are actually annotated with @safe. For example, invoking Array.withUnsafeBytes(_:) does not require an unsafe marker when -strict-memory-safety is specified: func process(_ values: [UInt8]) {   // warning: no unsafe operations occur within 'unsafe' expression   unsafe values.withUnsafeBytes { _ in     print("Processing \(values.count) bytes")   } } The call to withUnsafeBytes(_:) by itself is safe and the closure body performs no unsafe operations, so the outer unsafe is unnecessary. The unsafe operation would be using the buffer’s pointer inside the closure, which can be acknowledged closer to the source of the unsafety. How to fix Remove the unsafe marker from the outer expression. If a use inside the closure is the actually-unsafe operation, mark only that use: func process(_ values: [UInt8]) {   values.withUnsafeBytes { buffer in     let first = unsafe buffer.first ?? 0     print("First byte: \(first)")   } }

## See Also

### Related Documentation

- [Unnecessary effect markers (UnnecessaryEffectMarker)](swift-compiler/documentation/diagnostics/unnecessary-effect-marker.md)
- [Strict memory safety (StrictMemorySafety)](swift-compiler/documentation/diagnostics/strict-memory-safety.md)

- [@dynamicCallable implementation requirements (DynamicCallable)](swift-compiler/documentation/diagnostics/dynamic-callable-requirements.md)
- [Add @preconcurrency import (AddPreconcurrencyImport)](swift-compiler/documentation/diagnostics/add-preconcurrency-import.md)
- [Always enabled availability domains (AlwaysAvailableDomain)](swift-compiler/documentation/diagnostics/always-available-domain.md)
- [Argument matching for trailing closures (TrailingClosureMatching)](swift-compiler/documentation/diagnostics/trailing-closure-matching.md)
- [Calling a mutating async actor-isolated method (ActorIsolatedMutatingAsync)](swift-compiler/documentation/diagnostics/actor-isolated-mutating-async.md)
- [Calling an actor-isolated method from a synchronous nonisolated context (ActorIsolatedCall)](swift-compiler/documentation/diagnostics/actor-isolated-call.md)
- [Captures in a `@Sendable` closure (SendableClosureCaptures)](swift-compiler/documentation/diagnostics/sendable-closure-captures.md)
- [Compilation caching (CompilationCaching)](swift-compiler/documentation/diagnostics/compilation-caching.md)
- [Conforming to `StringInterpolationProtocol` (StringInterpolationConformance)](swift-compiler/documentation/diagnostics/string-interpolation-conformance.md)
- [Conversion from `@isolated(any)` function type to synchronous function type (ConversionFromIsolatedAnyToSynchronous)](swift-compiler/documentation/diagnostics/conversion-from-isolated-any-to-synchronous.md)
- [Cross-isolation data race (RegionIsolationCrossIsolationDataRace)](swift-compiler/documentation/diagnostics/region-isolation-cross-isolation-data-race.md)
- [Deprecated declaration warnings (DeprecatedDeclaration)](swift-compiler/documentation/diagnostics/deprecated-declaration.md)
- [Deprecated implementation-only imports (ImplementationOnlyDeprecated)](swift-compiler/documentation/diagnostics/implementation-only-deprecated.md)
- [Dynamic exclusivity (DynamicExclusivity)](swift-compiler/documentation/diagnostics/dynamic-exclusivity.md)
- [Embedded Swift language restrictions (EmbeddedRestrictions)](swift-compiler/documentation/diagnostics/embedded-restrictions.md)
