Merge pull request #1049 from golangci/unused-fix-mode
Fix unused in "fix" mode
This commit is contained in:
		
						commit
						c0618b0593
					
				| @ -33,7 +33,7 @@ func NewUnused() *goanalysis.Linter { | ||||
| 		for _, ur := range u.Result() { | ||||
| 			p := u.ProblemObject(lintCtx.Packages[0].Fset, ur) | ||||
| 			pkg := typesToPkg[ur.Pkg()] | ||||
| 			issues = append(issues, goanalysis.NewIssue(&result.Issue{ //nolint:scopelint | ||||
| 			i := &result.Issue{ | ||||
| 				FromLinter: name, | ||||
| 				Text:       p.Message, | ||||
| 				Pos:        p.Pos, | ||||
| @ -42,11 +42,16 @@ func NewUnused() *goanalysis.Linter { | ||||
| 					From: p.Pos.Line, | ||||
| 					To:   p.End.Line, | ||||
| 				}, | ||||
| 				Replacement: &result.Replacement{ | ||||
| 			} | ||||
| 			// See https://github.com/golangci/golangci-lint/issues/1048 | ||||
| 			// If range is invalid, this will break `--fix` mode. | ||||
| 			if i.LineRange.To >= i.LineRange.From { | ||||
| 				i.Replacement = &result.Replacement{ | ||||
| 					// Suggest deleting unused stuff. | ||||
| 					NeedOnlyDelete: true, | ||||
| 				}, | ||||
| 			}, nil)) | ||||
| 				} | ||||
| 			} | ||||
| 			issues = append(issues, goanalysis.NewIssue(i, nil)) | ||||
| 		} | ||||
| 		return issues | ||||
| 	}).WithContextSetter(func(lintCtx *linter.Context) { | ||||
|  | ||||
| @ -223,6 +223,12 @@ func (f Fixer) writeFixedFile(origFileLines [][]byte, issues []result.Issue, tmp | ||||
| 		} else { | ||||
| 			nextIssueIndex++ | ||||
| 			rng := nextIssue.GetLineRange() | ||||
| 			if rng.From > rng.To { | ||||
| 				// Maybe better decision is to skip such issues, re-evaluate if regressed. | ||||
| 				f.log.Warnf("[fixer]: issue line range is probably invalid, fix can be incorrect (from=%d, to=%d, linter=%s)", | ||||
| 					rng.From, rng.To, nextIssue.FromLinter, | ||||
| 				) | ||||
| 			} | ||||
| 			i += rng.To - rng.From | ||||
| 			if nextIssue.Replacement.NeedOnlyDelete { | ||||
| 				continue | ||||
|  | ||||
							
								
								
									
										8
									
								
								test/testdata/fix/in/unused.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								test/testdata/fix/in/unused.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | ||||
| //args: -Eunused | ||||
| package p | ||||
| 
 | ||||
| type ( | ||||
| 	unused struct{} | ||||
| ) | ||||
| 
 | ||||
| func X() {} | ||||
							
								
								
									
										8
									
								
								test/testdata/fix/out/unused.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								test/testdata/fix/out/unused.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | ||||
| //args: -Eunused | ||||
| package p | ||||
| 
 | ||||
| type ( | ||||
| 	unused struct{} | ||||
| ) | ||||
| 
 | ||||
| func X() {} | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Aleksandr Razumov
						Aleksandr Razumov