Pārlūkot izejas kodu

autofix: function call can be replaced with helper function (#6805)

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
deepsource-autofix[bot] 3 gadi atpakaļ
vecāks
revīzija
5afca6ca8e

+ 3 - 3
internal/auth/ldap/config.go

@@ -72,7 +72,7 @@ func (c *Config) sanitizedUserQuery(username string) (string, bool) {
 		return "", false
 	}
 
-	return strings.Replace(c.Filter, "%s", username, -1), true
+	return strings.ReplaceAll(c.Filter, "%s", username), true
 }
 
 func (c *Config) sanitizedUserDN(username string) (string, bool) {
@@ -83,7 +83,7 @@ func (c *Config) sanitizedUserDN(username string) (string, bool) {
 		return "", false
 	}
 
-	return strings.Replace(c.UserDN, "%s", username, -1), true
+	return strings.ReplaceAll(c.UserDN, "%s", username), true
 }
 
 func (c *Config) sanitizedGroupFilter(group string) (string, bool) {
@@ -112,7 +112,7 @@ func (c *Config) findUserDN(l *ldap.Conn, name string) (string, bool) {
 	log.Trace("Search for LDAP user: %s", name)
 	if len(c.BindDN) > 0 && len(c.BindPassword) > 0 {
 		// Replace placeholders with username
-		bindDN := strings.Replace(c.BindDN, "%s", name, -1)
+		bindDN := strings.ReplaceAll(c.BindDN, "%s", name)
 		err := l.Bind(bindDN, c.BindPassword)
 		if err != nil {
 			log.Trace("LDAP: Failed to bind as BindDN '%s': %v", bindDN, err)

+ 1 - 1
internal/cmd/import.go

@@ -95,7 +95,7 @@ func runImportLocale(c *cli.Context) error {
 			if idx > -1 && line[len(line)-1] == '"' {
 				// We still want the "=" sign
 				line = append(line[:idx+1], line[idx+2:len(line)-1]...)
-				line = bytes.Replace(line, escapedQuotes, regularQuotes, -1)
+				line = bytes.ReplaceAll(line, escapedQuotes, regularQuotes)
 			}
 			_, _ = tw.Write(line)
 			_, _ = tw.WriteString("\n")

+ 3 - 4
internal/db/ssh_key.go

@@ -112,10 +112,10 @@ func parseKeyString(content string) (string, error) {
 	// Transform all legal line endings to a single "\n"
 
 	// Replace all windows full new lines ("\r\n")
-	content = strings.Replace(content, "\r\n", "\n", -1)
+	content = strings.ReplaceAll(content, "\r\n", "\n")
 
 	// Replace all windows half new lines ("\r"), if it happen not to match replace above
-	content = strings.Replace(content, "\r", "\n", -1)
+	content = strings.ReplaceAll(content, "\r", "\n")
 
 	// Replace ending new line as its may cause unwanted behaviour (extra line means not a single line key | OpenSSH key)
 	content = strings.TrimRight(content, "\n")
@@ -374,8 +374,7 @@ func checkKeyContent(content string) error {
 
 func addKey(e Engine, key *PublicKey) (err error) {
 	// Calculate fingerprint.
-	tmpPath := strings.Replace(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
-		"id_rsa.pub"), "\\", "/", -1)
+	tmpPath := strings.ReplaceAll(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/")
 	_ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
 	if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil {
 		return err

+ 6 - 6
internal/db/webhook_slack.go

@@ -51,18 +51,18 @@ func (p *SlackPayload) JSONPayload() ([]byte, error) {
 // see: https://api.slack.com/docs/formatting
 func SlackTextFormatter(s string) string {
 	// replace & < >
-	s = strings.Replace(s, "&", "&amp;", -1)
-	s = strings.Replace(s, "<", "&lt;", -1)
-	s = strings.Replace(s, ">", "&gt;", -1)
+	s = strings.ReplaceAll(s, "&", "&amp;")
+	s = strings.ReplaceAll(s, "<", "&lt;")
+	s = strings.ReplaceAll(s, ">", "&gt;")
 	return s
 }
 
 func SlackShortTextFormatter(s string) string {
 	s = strings.Split(s, "\n")[0]
 	// replace & < >
-	s = strings.Replace(s, "&", "&amp;", -1)
-	s = strings.Replace(s, "<", "&lt;", -1)
-	s = strings.Replace(s, ">", "&gt;", -1)
+	s = strings.ReplaceAll(s, "&", "&amp;")
+	s = strings.ReplaceAll(s, "<", "&lt;")
+	s = strings.ReplaceAll(s, ">", "&gt;")
 	return s
 }
 

+ 1 - 1
internal/db/wiki.go

@@ -33,7 +33,7 @@ func ToWikiPageURL(name string) string {
 // that are not belong to wiki repository.
 func ToWikiPageName(urlString string) string {
 	name, _ := url.QueryUnescape(urlString)
-	return strings.Replace(strings.TrimLeft(path.Clean("/"+name), "/"), "/", " ", -1)
+	return strings.ReplaceAll(strings.TrimLeft(path.Clean("/"+name), "/"), "/", " ")
 }
 
 // WikiCloneLink returns clone URLs of repository wiki.

+ 3 - 4
internal/markup/markup.go

@@ -155,8 +155,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]strin
 	ms := MentionPattern.FindAll(rawBytes, -1)
 	for _, m := range ms {
 		m = m[bytes.Index(m, []byte("@")):]
-		rawBytes = bytes.Replace(rawBytes, m,
-			[]byte(fmt.Sprintf(`<a href="%s/%s">%s</a>`, conf.Server.Subpath, m[1:], m)), -1)
+		rawBytes = bytes.ReplaceAll(rawBytes, m, []byte(fmt.Sprintf(`<a href="%s/%s">%s</a>`, conf.Server.Subpath, m[1:], m)))
 	}
 
 	rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
@@ -216,7 +215,7 @@ func wrapImgWithLink(urlPrefix string, buf *bytes.Buffer, token html.Token) {
 	buf.WriteString(`">`)
 
 	if needPrepend {
-		src = strings.Replace(urlPrefix+src, " ", "%20", -1)
+		src = strings.ReplaceAll(urlPrefix+src, " ", "%20")
 		buf.WriteString(`<img src="`)
 		buf.WriteString(src)
 		buf.WriteString(`"`)
@@ -347,7 +346,7 @@ func Render(typ Type, input interface{}, urlPrefix string, metas map[string]stri
 		panic(fmt.Sprintf("unrecognized input content type: %T", input))
 	}
 
-	urlPrefix = strings.TrimRight(strings.Replace(urlPrefix, " ", "%20", -1), "/")
+	urlPrefix = strings.TrimRight(strings.ReplaceAll(urlPrefix, " ", "%20"), "/")
 	var rawHTML []byte
 	switch typ {
 	case TypeMarkdown:

+ 2 - 2
internal/route/install.go

@@ -240,7 +240,7 @@ func InstallPost(c *context.Context, f form.Install) {
 	}
 
 	// Test repository root path.
-	f.RepoRootPath = strings.Replace(f.RepoRootPath, "\\", "/", -1)
+	f.RepoRootPath = strings.ReplaceAll(f.RepoRootPath, "\\", "/")
 	if err := os.MkdirAll(f.RepoRootPath, os.ModePerm); err != nil {
 		c.FormErr("RepoRootPath")
 		c.RenderWithErr(c.Tr("install.invalid_repo_path", err), INSTALL, &f)
@@ -248,7 +248,7 @@ func InstallPost(c *context.Context, f form.Install) {
 	}
 
 	// Test log root path.
-	f.LogRootPath = strings.Replace(f.LogRootPath, "\\", "/", -1)
+	f.LogRootPath = strings.ReplaceAll(f.LogRootPath, "\\", "/")
 	if err := os.MkdirAll(f.LogRootPath, os.ModePerm); err != nil {
 		c.FormErr("LogRootPath")
 		c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), INSTALL, &f)

+ 1 - 1
internal/route/repo/editor.go

@@ -268,7 +268,7 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
 		OldTreeName:  oldTreePath,
 		NewTreeName:  f.TreePath,
 		Message:      message,
-		Content:      strings.Replace(f.Content, "\r", "", -1),
+		Content:      strings.ReplaceAll(f.Content, "\r", ""),
 		IsNewFile:    isNewFile,
 	}); err != nil {
 		log.Error("Failed to update repo file: %v", err)

+ 1 - 1
internal/route/repo/view.go

@@ -95,7 +95,7 @@ func renderDirectory(c *context.Context, treeLink string) {
 				c.Data["IsIPythonNotebook"] = true
 				c.Data["RawFileLink"] = c.Repo.RepoLink + "/raw/" + path.Join(c.Repo.BranchName, c.Repo.TreePath, readmeFile.Name())
 			default:
-				p = bytes.Replace(p, []byte("\n"), []byte(`<br>`), -1)
+				p = bytes.ReplaceAll(p, []byte("\n"), []byte(`<br>`))
 			}
 			c.Data["FileContent"] = string(p)
 		}

+ 1 - 1
internal/template/template.go

@@ -146,7 +146,7 @@ func Str2HTML(raw string) template.HTML {
 
 // NewLine2br simply replaces "\n" to "<br>".
 func NewLine2br(raw string) string {
-	return strings.Replace(raw, "\n", "<br>", -1)
+	return strings.ReplaceAll(raw, "\n", "<br>")
 }
 
 func Sha1(str string) string {