Skip to content

Run the VarDeclUsageChecker on top level code declarations. #6971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,15 @@ namespace swift {
///
/// \param WarnLongFunctionBodies If non-zero, warn when a function body takes
/// longer than this many milliseconds to type-check
///
/// \param DiagnoseTopLevelCode If true, perform diagnostics on all of the SF
/// top level declarations. This should only be done when parseIntoSourceFile
/// is done.
void performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
OptionSet<TypeCheckingFlags> Options,
unsigned StartElem = 0,
unsigned WarnLongFunctionBodies = 0);
unsigned WarnLongFunctionBodies = 0,
bool DiagnoseTopLevelCode = false);

/// Once type checking is complete, this walks protocol requirements
/// to resolve default witnesses.
Expand Down
3 changes: 2 additions & 1 deletion lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ void CompilerInstance::performSema() {
if (mainIsPrimary) {
performTypeChecking(MainFile, PersistentState.getTopLevelContext(),
TypeCheckOptions, CurTUElem,
options.WarnLongFunctionBodies);
options.WarnLongFunctionBodies,
/*DiagnoseTopLevelCode*/ Done);
}
CurTUElem = MainFile.Decls.size();
} while (!Done);
Expand Down
14 changes: 13 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,9 @@ class VarDeclUsageChecker : public ASTWalker {
VarDecls[param] = 0;

}


VarDeclUsageChecker(TypeChecker &TC) : TC(TC) {}

VarDeclUsageChecker(TypeChecker &TC, VarDecl *VD) : TC(TC) {
// Track a specific VarDecl
VarDecls[VD] = 0;
Expand Down Expand Up @@ -2390,6 +2392,16 @@ void swift::performAbstractFuncDeclDiagnostics(TypeChecker &TC,
AFD->getBody()->walk(VarDeclUsageChecker(TC, AFD));
}

void swift::performTopLevelDeclDiagnostics(TypeChecker &TC,
ArrayRef<Decl*> topLevel) {
VarDeclUsageChecker checker(TC);
for (auto D : topLevel) {
if (TopLevelCodeDecl *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
TLCD->getBody()->walk(checker);
}
}
}

/// Diagnose C style for loops.

namespace {
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/MiscDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

namespace swift {
class AbstractFunctionDecl;
class TopLevelCodeDecl;
class ApplyExpr;
class CallExpr;
class Decl;
class DeclContext;
class Expr;
class InFlightDiagnostic;
Expand All @@ -41,6 +43,8 @@ void performStmtDiagnostics(TypeChecker &TC, const Stmt *S);

void performAbstractFuncDeclDiagnostics(TypeChecker &TC,
AbstractFunctionDecl *AFD);

void performTopLevelDeclDiagnostics(TypeChecker &TC, ArrayRef<Decl*> topLevel);

/// Emit a fix-it to set the accessibility of \p VD to \p desiredAccess.
///
Expand Down
7 changes: 6 additions & 1 deletion lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "swift/Subsystems.h"
#include "TypeChecker.h"
#include "MiscDiagnostics.h"
#include "GenericTypeResolver.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ASTVisitor.h"
Expand Down Expand Up @@ -594,7 +595,8 @@ void swift::typeCheckExternalDefinitions(SourceFile &SF) {
void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
OptionSet<TypeCheckingFlags> Options,
unsigned StartElem,
unsigned WarnLongFunctionBodies) {
unsigned WarnLongFunctionBodies,
bool DiagnoseTopLevelCode) {
if (SF.ASTStage == SourceFile::TypeChecked)
return;

Expand Down Expand Up @@ -681,6 +683,9 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
}

if (hasTopLevelCode) {
if (DiagnoseTopLevelCode) {
performTopLevelDeclDiagnostics(TC, llvm::makeArrayRef(SF.Decls));
}
TC.contextualizeTopLevelCode(TLC,
llvm::makeArrayRef(SF.Decls).slice(StartElem));
}
Expand Down
15 changes: 15 additions & 0 deletions test/decl/var/usage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,18 @@ func test2() {
var c: Int = 4 // expected-warning {{variable 'c' was never used; consider replacing with '_' or removing it}} {{7-8=_}}
let (d): Int = 9 // expected-warning {{immutable value 'd' was never used; consider replacing with '_' or removing it}} {{8-9=_}}
}

for i in 0..<10 { // expected-warning {{immutable value 'i' was never used; consider replacing with '_' or removing it}} {{5-6=_}}
print("")
}


let optionalString: String? = "check"
if let string = optionalString {} // expected-warning {{value 'string' was defined but never used; consider replacing with boolean test}} {{4-17=}} {{31-31= != nil}}

var unNeededVar = false // expected-warning {{variable 'unNeededVar' was never mutated; consider changing to 'let' constant}} {{1-4=let}}
if unNeededVar {}

let optionalAny: Any? = "check"
if let string = optionalAny as? String {} // expected-warning {{value 'string' was defined but never used; consider replacing with boolean test}} {{4-17=(}} {{39-39=) != nil}}