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

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
pull/6811/head
deepsource-autofix[bot] 2022-03-06 16:33:55 +08:00 committed by GitHub
parent deec3516d5
commit 5afca6ca8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 24 deletions

View File

@ -72,7 +72,7 @@ func (c *Config) sanitizedUserQuery(username string) (string, bool) {
return "", false 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) { func (c *Config) sanitizedUserDN(username string) (string, bool) {
@ -83,7 +83,7 @@ func (c *Config) sanitizedUserDN(username string) (string, bool) {
return "", false 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) { 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) log.Trace("Search for LDAP user: %s", name)
if len(c.BindDN) > 0 && len(c.BindPassword) > 0 { if len(c.BindDN) > 0 && len(c.BindPassword) > 0 {
// Replace placeholders with username // 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) err := l.Bind(bindDN, c.BindPassword)
if err != nil { if err != nil {
log.Trace("LDAP: Failed to bind as BindDN '%s': %v", bindDN, err) log.Trace("LDAP: Failed to bind as BindDN '%s': %v", bindDN, err)

View File

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

View File

@ -112,10 +112,10 @@ func parseKeyString(content string) (string, error) {
// Transform all legal line endings to a single "\n" // Transform all legal line endings to a single "\n"
// Replace all windows full new lines ("\r\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 // 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) // 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") content = strings.TrimRight(content, "\n")
@ -374,8 +374,7 @@ func checkKeyContent(content string) error {
func addKey(e Engine, key *PublicKey) (err error) { func addKey(e Engine, key *PublicKey) (err error) {
// Calculate fingerprint. // Calculate fingerprint.
tmpPath := strings.Replace(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), tmpPath := strings.ReplaceAll(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/")
"id_rsa.pub"), "\\", "/", -1)
_ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm) _ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil { if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil {
return err return err

View File

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

View File

@ -33,7 +33,7 @@ func ToWikiPageURL(name string) string {
// that are not belong to wiki repository. // that are not belong to wiki repository.
func ToWikiPageName(urlString string) string { func ToWikiPageName(urlString string) string {
name, _ := url.QueryUnescape(urlString) 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. // WikiCloneLink returns clone URLs of repository wiki.

View File

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

View File

@ -240,7 +240,7 @@ func InstallPost(c *context.Context, f form.Install) {
} }
// Test repository root path. // 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 { if err := os.MkdirAll(f.RepoRootPath, os.ModePerm); err != nil {
c.FormErr("RepoRootPath") c.FormErr("RepoRootPath")
c.RenderWithErr(c.Tr("install.invalid_repo_path", err), INSTALL, &f) 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. // 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 { if err := os.MkdirAll(f.LogRootPath, os.ModePerm); err != nil {
c.FormErr("LogRootPath") c.FormErr("LogRootPath")
c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), INSTALL, &f) c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), INSTALL, &f)

View File

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

View File

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

View File

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