Option Explicit or Option Strict is switched off in this file

ID

vbnet.maintainability.option_explicit_enabled

Severity

high

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Type Design

Language

VB.NET

Description

Reports an Option Explicit Off or Option Strict Off statement, which switches off the compiler’s declaration checking and its type checking for the whole file. Only the explicit Off statements are reported. A file that simply omits the directive is not reported, because that state is not visible in the file - the effective setting then comes from the project, where Option Explicit and Option Strict are set for every file at once, so a project configured with Off produces no finding here. Check the project settings as well as the files. Option Compare and Option Infer govern different things and are not reported.

Rationale

Both directives turn compile-time errors into run-time behaviour. With Option Explicit Off, any misspelled identifier is silently accepted as a new implicitly declared Object variable, so custmerTotal = total compiles, assigns nothing anyone reads, and leaves the correctly spelled variable at its previous value - a defect no amount of review reliably catches, because the code looks right and the compiler agrees. With Option Strict Off the compiler permits late binding and implicit narrowing conversions: a member name is resolved at run time, so a typo in a property name or a rename that misses one call site fails as a MissingMemberException in production rather than at build time, and a narrowing conversion that loses data or overflows raises InvalidCastException or OverflowException on the first value that does not fit. The cost is not only correctness. Late binding is resolved by reflection on every call, which is far slower than a direct call, and it removes the type information the tooling depends on, so IntelliSense, "find all references" and rename refactoring stop being reliable across the file - which is what makes such files progressively harder to change.

The following code illustrates the pattern detected by this rule:

' FLAGGED: Option Explicit or Option Strict is switched off in this file
Option Explicit Off
' FLAGGED: Option Explicit or Option Strict is switched off in this file
Option Strict Off

Remediation

Set both directives to On, at the project level so they cover every file - the Option Explicit and Option Strict settings in the project’s compile options - and delete the per-file Off statements. Expect the build to fail on the first attempt, and treat those failures as the point of the change: declare every implicitly created variable with Dim …​ As <Type>, replace late-bound member access on an Object with a cast to the real type or an interface, and make every narrowing conversion explicit with CInt, CDec, DirectCast or TryCast so the intent is stated and the failure is visible. For a large legacy file, turning Option Strict On for the file first and fixing it in isolation keeps the change reviewable; where a genuinely dynamic call has to survive, confine it behind a small typed wrapper method rather than leaving the whole file unchecked.

Configuration

This detector does not need any configuration.