Commit Graph

59 Commits

Author SHA1 Message Date
Doug Gregor
d9ef00ce16 Look for keywords in inactive #if regions when checking for variable uses
Fixes issue #79555
2025-02-24 18:04:31 -08:00
Peter Wong
c1473e84f9 [Diagnostics] Improve warning suggestion for var in for loop
When iterator consists of tuple of variable and iteration only mutates
the tuple partially, improve the warning message from "changing to 'let"
to "changing the pattern to '(..., case let, ...)"
2024-12-10 22:51:08 +08:00
Doug Gregor
70c93c63b3 Add another fixed test case provided by Rintaro (thanks!) 2024-09-06 22:52:24 -07:00
Doug Gregor
4a4c0f68f1 Teach the "unused variable / value" diagnostics to use SwiftIfConfig
Rather than walking into the inactive regions of IfConfigDecls looking for
references to a declaration before we diagnose it, go to the syntax
tree and look through inactive *and unparsed* regions for identifier
tokens that match. If we find one, suppress the diagnostic.

This reduces our dependency on IfConfigDecl in the AST, and also makes
the same suppression work with code in unparsed regions that had no
representation in IfConfigDecl.
2024-09-06 22:14:52 -07:00
Anthony Latsis
97e122fee7 Gardening: Migrate test suite to GH issues: decl/var 2022-08-26 04:09:10 +03:00
Ikko Ashimine
48704e10fe Fix typo in usage.swift
occuring -> occurring
2021-12-02 23:51:51 +09:00
Pavel Yaskevich
031d132e77 Fix SR Number in test/decl/var/usage.swift
Co-authored-by: Luciano Almeida <passos.luciano@outlook.com>
2021-06-17 09:58:08 -07:00
Minhyuk Kim
f85cb50af2 [Sema] Add diagnostics handling for nested expressions 2021-06-17 11:25:51 +09:00
Minhyuk Kim
c3ab99a405 [Sema] Offer 'is' replacement for unused 'if let' expression when the operand is optional 2021-06-14 00:40:03 +09:00
Slava Pestov
4f1ab9d8e5 ASTScope: Directly calculate end location of GuardStmtScope and PatternEntryDeclScope
Today, the reported source range of a GuardStmtScope is just that of
the statement itself, ending after the 'else' block. Similarly, a
PatternEntryDeclScope ends immediately after the initializer
expression.

Since these scopes introduce names until the end of the parent
BraceStmt (meaning they change the insertion point), we were
relying on the addition of child scopes to extend the source
range.

While we still need the hack for extending source ranges to deal
with string interpolation, at least in the other cases we can
get rid of it.

Note that this fixes a bug where a GuardStmtScope would end
before an inactive '#if' block if the inactive '#if' block was
the last element of a BraceStmt.
2020-10-02 23:49:59 -04:00
Robert Widmann
8a7a1d98f5 Teach the VarDeclUsageChecker About Variables Bound in Patterns
The VDUC was missing a class of AST nodes that can bind variables:
patterns in switch statements. For these, it was falling back to
requesting a simple replacement of the bound variable name with _. But
for patterns, this means there's a two-step dance the user has to go
through where the first fixit does this:

.pattern(let x) -> .pattern(let _)

Then a second round of compilation would emit a fixit to do this:

.pattern(let _) -> .pattern(_)

Instead, detect "simple" variable bindings - for now, variable patterns
that are immediately preceded by a `let` or `var` binding pattern - and
collapse two steps into one.

Resolves rdar://47240768
2020-07-21 12:33:14 -07:00
Slava Pestov
2c2d087242 Sema: More accurate VarDeclUsageChecker analysis with local functions
We used to take all the captures of a local function and treat them all
as read and write usages of vars from an outer scope. Instead, let's
refactor the analysis to walk into local functions.
2020-04-03 18:07:08 -04:00
Slava Pestov
93f0faf109 Sema: Fix false-positive var => let warning in VarUsageDeclChecker
Fixes <rdar://problem/60563962>.
2020-04-03 17:27:43 -04:00
Slava Pestov
2ef101c815 Sema: Don't add local functions to TC.definedFunctions
Instead, check them and their error handling right away.

In addition to fixing the crash in the radar, this also causes
us to emit unused variable warnings in functions containing
local functions.

Eventually, TC.definedFunctions should go away altogether.

Fixes <rdar://problem/53956342>.
2019-08-07 00:37:21 -04:00
Sam Lazarus
ede8127adf Test: Add and update tests for allowing var and let as argument labels 2019-04-26 04:08:27 -04:00
John McCall
4f4d64b93e Various improvements to the variable-is-never-mutated diagnostic.
The main fixes here are:
- we weren't looking through open-existentials in the l-value
- we weren't handling mutating gets correctly unless CSApply wrapped
  the base in an InOutExpr, which seems to be multifile-sensitive
- we were missing diagnostics in some cases involving subscripts

A better fix would be to re-introduce LValueAccessKind, but I wanted
a workable short-term fix that I could try to get into 5.1.

Fixes rdar://49482742, which is specific to the lazy-getter problem.
2019-04-08 18:43:24 -04:00
Jordan Rose
aabcabfdf3 Merge pull request #21983 from KingOfBrian/bugfix/SR-964-As-Member-master
[Sema] Report unused newValue when getter is accessed in custom setter
2019-02-04 14:46:33 -08:00
Brian King
a75f616084 Move extension check out of function 2019-01-30 15:03:14 -05:00
Ding Ye
421d6b065a [Sema] Improve diagnostics for variable_never_mutated in case of for-each loop's binding.
The diagnostics for `variable_never_mutated` always suggests
changing `var` to `let`, which is misleading in case of
for-each loops where explicitly immutable context applies.
This patch adds some variety to the message to make it appropriate.

Resolves: SR-9732
2019-01-24 16:54:57 +11:00
Brian King
9aa48ce540 Add class and extension tests. 2019-01-22 12:18:34 -05:00
Brian King
3c6fc86c5a Check for getter by member reference 2019-01-18 09:49:23 -05:00
Brian King
a5a6684fce Improve diagnostic language 2017-09-26 20:47:09 -04:00
Brian King
4f20f670e7 Generate a fix-it to change the first getter usage into the setter parameter. 2017-09-25 20:19:36 -04:00
Brian King
b894bd038b Update error message 2017-09-25 19:21:16 -04:00
Brian King
f1f64ce8a6 Change warning to only be reported if the parameter is unused but the getter is used. 2017-09-25 19:20:58 -04:00
Robert Widmann
43f29399f3 Remove an old diagnostic for 'var' in param position
Using the attribute in this position is a relic from the Swift 2
days, and fixing it required letting invalid code fall through to
Sema instead of being diagnosed in Parse proper.  Treat 'var'
in this position like 'let' by simply offering to remove it
instead of extracting it into a separate variable.
2017-06-29 13:35:59 -07:00
Pavel Yaskevich
6180ecef73 [QoI] Don't suggest var to let when binding inside for-statement
Instead of doing that, suggest removing 'var' altogether because
'let' cannot be used directly in an implicitly immutable context.

Resolves: rdar://problem/32390726
2017-06-09 01:10:40 -07:00
David Hart
13edc600f0 [SR-2421] Remove variable_never_used fixit to work with two-stage let initialization
The variable_never_used fixit transforms into invalid code in the case of two-stage let initialization. I introduced a new diagnostic that does not fixit and suggests removing the value.
2017-03-18 00:21:18 +01:00
Brian King
0c57aebfea Fix unit tests 2017-01-26 10:04:41 -05:00
Brian King
edc193efa0 Fix warnings in top level statements 2017-01-24 22:53:21 -05:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Rintaro Ishizaki
f4a9898142 [QoI] Improve "never used" diagnostics (#5638)
On 'let (x) = some', we should remove r-paren as well.
On 'let x: Int = some', we can't remove 'let' introducer.
2016-11-05 15:17:54 +09:00
Robert Widmann
2a5c7ddf90 Quick fix for off-by-count errors in pattern binding diagnostics
Rather than return the last var pattern, only match on the ones that
match the vardecl the diagnostic is warning about.
2016-07-10 22:32:08 -07:00
Chris Lattner
45f2cfaaa0 Implement SE-0099, but where the migration diagnostics are left as warnings
for now.  I'll upgrade them to errors in a week or two to give downstream
projects a chance to update.
2016-07-02 15:44:57 -07:00
Jordan Rose
bc83940301 Make pointer nullability explicit using Optional.
Implements SE-0055: https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md

- Add NULL as an extra inhabitant of Builtin.RawPointer (currently
  hardcoded to 0 rather than being target-dependent).
- Import non-object pointers as Optional/IUO when nullable/null_unspecified
  (like everything else).
- Change the type checker's *-to-pointer conversions to handle a layer of
  optional.
- Use 'AutoreleasingUnsafeMutablePointer<NSError?>?' as the type of error
  parameters exported to Objective-C.
- Drop NilLiteralConvertible conformance for all pointer types.
- Update the standard library and then all the tests.

I've decided to leave this commit only updating existing tests; any new
tests will come in the following commits. (That may mean some additional
implementation work to follow.)

The other major piece that's missing here is migration. I'm hoping we get
a lot of that with Swift 1.1's work for optional object references, but
I still need to investigate.
2016-04-11 20:06:38 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Joe Groff
07152b51e5 Fix a couple of issues with the fixit for unused if let bindings.
- Fix SR-1112, where the fixit would eat all characters in the RHS expression except for the last one due to improper SourceRange math.
- Noticed by inspection, an expression undergoing an implicit conversion (such as a load from an optional `var`) would be judged as requiring parens, when that's really unnecessary.
2016-03-30 15:09:01 -07:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
David Farler
8a5ed405bf Make var parameters an error for Swift 3
This finishes up revisions to SE-0003 - only var function parameters
are disallowed for Swift 3.
2016-01-30 12:39:17 -08:00
David Farler
3f635d04c7 Reinstante var bindings in refutable patterns, except function parameters.
This reverts commits: b96e06da44,
                      8f2fbdc93a,
                      93b6962478,
                      64024118f4,
                      a759ca9141,
                      3434f9642b,
                      9f33429891,
                      47c043e8a6.

This commit leaves 'var' on function parameters as a warning to be
merged into Swift 2.2. For Swift 3, this will be an error, to be
converted in a follow-up.
2016-01-29 15:27:08 -08:00
David Farler
a6f2530780 Revert "REVERTME: Temporarily make vars in refutable patterns a warning"
This reverts commit b96e06da44, making
vars in refutable patterns an error for Swift 3.

rdar://problem/23172698
2016-01-14 20:52:24 -08:00
David Farler
b96e06da44 REVERTME: Temporarily make vars in refutable patterns a warning
Revert "Make function parameters and refutable patterns always
immutable"

This reverts commit 8f2fbdc93a.

Once we have finally merged master into the Swift 2.2 branch to be, we
should revert this commit to turn the errors back on for Swift 3.0.
2015-12-05 23:13:04 -08:00
ohkawa
f5caed81fd Fix typo 2015-12-04 21:58:15 +09:00
Chris Lattner
2379928d07 Fix <rdar://22774938> QoI: "never used" in an "if let" should rewrite expression to use != nil
When we see an unused variable in a simple-enough "if/let" (also guard and
while of course), fixit it into a comparison against nil instead of replacing
the name of the variable with "_".  Also special case initialization with an
as? expression, since we can transform that into an "is" boolean test.
2015-11-29 21:49:36 -08:00
David Farler
8f2fbdc93a Make function parameters and refutable patterns always immutable
All refutable patterns and function parameters marked with 'var'
is now an error.

- Using explicit 'let' keyword on function parameters causes a warning.
- Don't suggest making function parameters mutable
- Remove uses in the standard library
- Update tests

rdar://problem/23378003
2015-11-09 16:56:13 -08:00
David Farler
93b6962478 Warn when using 'var' bindings in function parameters
These will no longer be allowed in a future Swift release.

rdar://problem/23172698
2015-11-03 17:24:20 -08:00
David Farler
64024118f4 Make always-immutable pattern bindings a warning for now
These will become errors after the next release branch.

rdar://problem/23172698
2015-11-03 12:46:38 -08:00
David Farler
a759ca9141 Disallow 'var' bindings in case patterns
Make the following illegal:

switch thing {
  case .A(var x):
    modify(x0
}

And provide a replacement 'var' -> 'let' fix-it.

rdar://problem/23172698

Swift SVN r32883
2015-10-25 18:53:02 +00:00
David Farler
3434f9642b Disallow 'var' pattern bindings in if, while, and guard statements
Make the following patterns illegal:

  if var x = ... {
    ...
  }

  guard var x = ... else {
    ...
  }

  while var x = ... {
    ...
  }

And provide a replacement fixit 'var' -> 'let'.

rdar://problem/23172698

Swift SVN r32855
2015-10-24 01:46:30 +00:00
David Farler
9f33429891 Tweak var in for-in statement diagnostic
Actually say that `var` is not allowed in a for-in statement.

rdar://problem/23172698

Swift SVN r32827
2015-10-22 18:07:04 +00:00