autofix: fix check for empty string (#6804)

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
This commit is contained in:
deepsource-autofix[bot]
2022-03-06 16:33:45 +08:00
committed by GitHub
parent 65526f84e1
commit deec3516d5
39 changed files with 82 additions and 86 deletions

View File

@@ -34,7 +34,7 @@ func Repos(c *context.Context) {
)
keyword := c.Query("q")
if len(keyword) == 0 {
if keyword == "" {
repos, err = db.Repositories(page, conf.UI.Admin.RepoPagingNum)
if err != nil {
c.Error(err, "list repositories")

View File

@@ -12,7 +12,7 @@ import (
)
func Markdown(c *context.APIContext, form api.MarkdownOption) {
if len(form.Text) == 0 {
if form.Text == "" {
_, _ = c.Write([]byte(""))
return
}

View File

@@ -142,7 +142,7 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
if c.Repo.IsWriter() && form.Assignee != nil &&
(issue.Assignee == nil || issue.Assignee.LowerName != strings.ToLower(*form.Assignee)) {
if len(*form.Assignee) == 0 {
if *form.Assignee == "" {
issue.AssigneeID = 0
} else {
assignee, err := db.GetUserByName(*form.Assignee)

View File

@@ -38,7 +38,7 @@ func Home(c *context.Context) {
// Check auto-login.
uname := c.GetCookie(conf.Security.CookieUsername)
if len(uname) != 0 {
if uname != "" {
c.Redirect(conf.Server.Subpath + "/user/login")
return
}
@@ -104,7 +104,7 @@ func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
)
keyword := c.Query("q")
if len(keyword) == 0 {
if keyword == "" {
users, err = opts.Ranger(page, opts.PageSize)
if err != nil {
c.Error(err, "ranger")

View File

@@ -221,7 +221,7 @@ func InstallPost(c *context.Context, f form.Install) {
conf.Database.SSLMode = f.SSLMode
conf.Database.Path = f.DbPath
if conf.Database.Type == "sqlite3" && len(conf.Database.Path) == 0 {
if conf.Database.Type == "sqlite3" && conf.Database.Path == "" {
c.FormErr("DbPath")
c.RenderWithErr(c.Tr("install.err_empty_db_path"), INSTALL, &f)
return
@@ -280,14 +280,14 @@ func InstallPost(c *context.Context, f form.Install) {
}
// Check logic loophole between disable self-registration and no admin account.
if f.DisableRegistration && len(f.AdminName) == 0 {
if f.DisableRegistration && f.AdminName == "" {
c.FormErr("Services", "Admin")
c.RenderWithErr(c.Tr("install.no_admin_and_disable_registration"), INSTALL, f)
return
}
// Check admin password.
if len(f.AdminName) > 0 && len(f.AdminPasswd) == 0 {
if len(f.AdminName) > 0 && f.AdminPasswd == "" {
c.FormErr("Admin", "AdminPasswd")
c.RenderWithErr(c.Tr("install.err_empty_admin_password"), INSTALL, f)
return

View File

@@ -25,7 +25,7 @@ const (
func RefCommits(c *context.Context) {
c.Data["PageIsViewFiles"] = true
switch {
case len(c.Repo.TreePath) == 0:
case c.Repo.TreePath == "":
Commits(c)
case c.Repo.TreePath == "search":
SearchCommits(c)
@@ -85,7 +85,7 @@ func SearchCommits(c *context.Context) {
c.Data["PageIsCommits"] = true
keyword := c.Query("q")
if len(keyword) == 0 {
if keyword == "" {
c.Redirect(c.Repo.RepoLink + "/commits/" + c.Repo.BranchName)
return
}

View File

@@ -33,7 +33,7 @@ const (
// getParentTreeFields returns list of parent tree names and corresponding tree paths
// based on given tree path.
func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
if len(treePath) == 0 {
if treePath == "" {
return treeNames, treePaths
}
@@ -158,7 +158,7 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
return
}
if len(f.TreePath) == 0 {
if f.TreePath == "" {
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.filename_cannot_be_empty"), tmplEditorEdit, &f)
return
@@ -248,7 +248,7 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
}
message := strings.TrimSpace(f.CommitSummary)
if len(message) == 0 {
if message == "" {
if isNewFile {
message = c.Tr("repo.editor.add", f.TreePath)
} else {
@@ -362,7 +362,7 @@ func DeleteFilePost(c *context.Context, f form.DeleteRepoFile) {
}
message := strings.TrimSpace(f.CommitSummary)
if len(message) == 0 {
if message == "" {
message = c.Tr("repo.editor.delete", c.Repo.TreePath)
}
@@ -481,7 +481,7 @@ func UploadFilePost(c *context.Context, f form.UploadRepoFile) {
}
message := strings.TrimSpace(f.CommitSummary)
if len(message) == 0 {
if message == "" {
message = c.Tr("repo.editor.upload_files_to_dir", f.TreePath)
}
@@ -555,7 +555,7 @@ func UploadFileToServer(c *context.Context) {
}
func RemoveUploadFileFromServer(c *context.Context, f form.RemoveUploadFile) {
if len(f.File) == 0 {
if f.File == "" {
c.Status(http.StatusNoContent)
return
}

View File

@@ -106,7 +106,7 @@ func HTTPContexter() macaron.Handler {
// Handle HTTP Basic Authentication
authHead := c.Req.Header.Get("Authorization")
if len(authHead) == 0 {
if authHead == "" {
askCredentials(c, http.StatusUnauthorized, "")
return
}

View File

@@ -700,7 +700,7 @@ func UpdateIssueTitle(c *context.Context) {
}
title := c.QueryTrim("title")
if len(title) == 0 {
if title == "" {
c.Status(http.StatusNoContent)
return
}
@@ -903,7 +903,7 @@ func NewComment(c *context.Context, f form.CreateComment) {
}()
// Fix #321: Allow empty comments, as long as we have attachments.
if len(f.Content) == 0 && len(attachments) == 0 {
if f.Content == "" && len(attachments) == 0 {
return
}
@@ -933,7 +933,7 @@ func UpdateCommentContent(c *context.Context) {
oldContent := comment.Content
comment.Content = c.Query("content")
if len(comment.Content) == 0 {
if comment.Content == "" {
c.JSONSuccess(map[string]interface{}{
"content": "",
})
@@ -1127,7 +1127,7 @@ func NewMilestonePost(c *context.Context, f form.CreateMilestone) {
return
}
if len(f.Deadline) == 0 {
if f.Deadline == "" {
f.Deadline = "9999-12-31"
}
deadline, err := time.ParseInLocation("2006-01-02", f.Deadline, time.Local)
@@ -1183,7 +1183,7 @@ func EditMilestonePost(c *context.Context, f form.CreateMilestone) {
return
}
if len(f.Deadline) == 0 {
if f.Deadline == "" {
f.Deadline = "9999-12-31"
}
deadline, err := time.ParseInLocation("2006-01-02", f.Deadline, time.Local)

View File

@@ -293,7 +293,7 @@ func EditReleasePost(c *context.Context, f form.EditRelease) {
attachments = f.Files
}
isPublish := rel.IsDraft && len(f.Draft) == 0
isPublish := rel.IsDraft && f.Draft == ""
rel.Title = f.Title
rel.Note = f.Content
rel.IsDraft = len(f.Draft) > 0

View File

@@ -378,7 +378,7 @@ func SettingsCollaboration(c *context.Context) {
func SettingsCollaborationPost(c *context.Context) {
name := strings.ToLower(c.Query("collaborator"))
if len(name) == 0 || c.Repo.Owner.LowerName == name {
if name == "" || c.Repo.Owner.LowerName == name {
c.Redirect(conf.Server.Subpath + c.Req.URL.Path)
return
}

View File

@@ -177,7 +177,7 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
var output bytes.Buffer
lines := strings.Split(fileContent, "\n")
// Remove blank line at the end of file
if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
if len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
for index, line := range lines {

View File

@@ -75,7 +75,7 @@ func renderWikiPage(c *context.Context, isViewPage bool) (*git.Repository, strin
}
pageURL := c.Params(":page")
if len(pageURL) == 0 {
if pageURL == "" {
pageURL = "Home"
}
c.Data["PageURL"] = pageURL
@@ -253,7 +253,7 @@ func EditWikiPost(c *context.Context, f form.NewWiki) {
func DeleteWikiPagePost(c *context.Context) {
pageURL := c.Params(":page")
if len(pageURL) == 0 {
if pageURL == "" {
pageURL = "Home"
}

View File

@@ -38,7 +38,7 @@ func AutoLogin(c *context.Context) (bool, error) {
}
uname := c.GetCookie(conf.Security.CookieUsername)
if len(uname) == 0 {
if uname == "" {
return false, nil
}
@@ -384,7 +384,7 @@ func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) {
func Activate(c *context.Context) {
code := c.Query("code")
if len(code) == 0 {
if code == "" {
c.Data["IsActivatePage"] = true
if c.User.IsActive {
c.NotFound()
@@ -515,7 +515,7 @@ func ResetPasswd(c *context.Context) {
c.Title("auth.reset_password")
code := c.Query("code")
if len(code) == 0 {
if code == "" {
c.NotFound()
return
}
@@ -528,7 +528,7 @@ func ResetPasswdPost(c *context.Context) {
c.Title("auth.reset_password")
code := c.Query("code")
if len(code) == 0 {
if code == "" {
c.NotFound()
return
}