diff options
author | rillig <rillig@pkgsrc.org> | 2022-07-06 06:14:24 +0000 |
---|---|---|
committer | rillig <rillig@pkgsrc.org> | 2022-07-06 06:14:24 +0000 |
commit | 5e246deadbef22b21c9385d13d6812865b36d509 (patch) | |
tree | bb863c99a512e744387c74177b986b7b31d90d9a /pkgtools/pkglint | |
parent | 8a620823bda7618b3eca5a0f80c7a8122a0ba836 (diff) | |
download | pkgsrc-5e246deadbef22b21c9385d13d6812865b36d509.tar.gz |
pkgtools/pkglint: update to 22.2.1
Changes since 22.2.0:
Suggest simpler condition when matching a variable against a pattern
(occurs 220 times in pkgsrc).
Improve explanation for documenting patches.
Diffstat (limited to 'pkgtools/pkglint')
-rw-r--r-- | pkgtools/pkglint/Makefile | 5 | ||||
-rw-r--r-- | pkgtools/pkglint/files/mkcondchecker.go | 32 | ||||
-rw-r--r-- | pkgtools/pkglint/files/mkcondchecker_test.go | 25 | ||||
-rw-r--r-- | pkgtools/pkglint/files/patches.go | 3 |
4 files changed, 49 insertions, 16 deletions
diff --git a/pkgtools/pkglint/Makefile b/pkgtools/pkglint/Makefile index af477b6e9cf..b331a426c2b 100644 --- a/pkgtools/pkglint/Makefile +++ b/pkgtools/pkglint/Makefile @@ -1,7 +1,6 @@ -# $NetBSD: Makefile,v 1.719 2022/06/28 11:35:26 wiz Exp $ +# $NetBSD: Makefile,v 1.720 2022/07/06 06:14:24 rillig Exp $ -PKGNAME= pkglint-22.2.0 -PKGREVISION= 1 +PKGNAME= pkglint-22.2.1 CATEGORIES= pkgtools DISTNAME= tools MASTER_SITES= ${MASTER_SITE_GITHUB:=golang/} diff --git a/pkgtools/pkglint/files/mkcondchecker.go b/pkgtools/pkglint/files/mkcondchecker.go index 8e8d4c768d1..b652df90ac8 100644 --- a/pkgtools/pkglint/files/mkcondchecker.go +++ b/pkgtools/pkglint/files/mkcondchecker.go @@ -168,14 +168,13 @@ var mkCondModifierPatternLiteral = textproc.NewByteSet("-+,./0-9<=>@A-Z_a-z") // * neg is true for the form !empty(VAR...), and false for empty(VAR...). func (ck *MkCondChecker) simplify(varuse *MkVarUse, fromEmpty bool, neg bool) { varname := varuse.varname - mods := varuse.modifiers - modifiers := mods + modifiers := varuse.modifiers n := len(modifiers) if n == 0 { return } - modsExceptLast := NewMkVarUse("", mods[:n-1]...).Mod() + modsExceptLast := NewMkVarUse("", modifiers[:n-1]...).Mod() vartype := G.Pkgsrc.VariableType(ck.MkLines, varname) isDefined := func() bool { @@ -226,6 +225,7 @@ func (ck *MkCondChecker) simplify(varuse *MkVarUse, fromEmpty bool, neg bool) { condStr(fromEmpty, ")", "}")) needsQuotes := textproc.NewLexer(pattern).NextBytesSet(mkCondStringLiteralUnquoted) != pattern || + pattern == "" || matches(pattern, `^\d+\.?\d*$`) quote := condStr(needsQuotes, "\"", "") @@ -242,6 +242,32 @@ func (ck *MkCondChecker) simplify(varuse *MkVarUse, fromEmpty bool, neg bool) { return } + // Replace !empty(VAR:M*.c) with ${VAR:M*.c}. + // Replace empty(VAR:M*.c) with !${VAR:M*.c}. + if fromEmpty && positive && !exact && vartype != nil && isDefined() && + // Restrict replacements to very simple patterns with only few + // special characters. + // Before generalizing this to arbitrary strings, there has to be + // a proper code generator for these conditions that handles all + // possible escaping. + matches(varuse.Mod(), `^[*.:\w]+$`) { + + fixedPart := varname + modsExceptLast + ":M" + pattern + from := condStr(neg, "!", "") + "empty(" + fixedPart + ")" + to := condStr(neg, "", "!") + "${" + fixedPart + "}" + + fix := ck.MkLine.Autofix() + fix.Notef("%q can be simplified to %q.", from, to) + fix.Explain( + "This variable is guaranteed to be defined at this point.", + "Therefore it may occur on the left-hand side of a comparison", + "and doesn't have to be guarded by the function 'empty'.") + fix.Replace(from, to) + fix.Apply() + + return + } + switch { case !exact, vartype == nil, diff --git a/pkgtools/pkglint/files/mkcondchecker_test.go b/pkgtools/pkglint/files/mkcondchecker_test.go index 7cdaaf1b2cf..fe83684a8e7 100644 --- a/pkgtools/pkglint/files/mkcondchecker_test.go +++ b/pkgtools/pkglint/files/mkcondchecker_test.go @@ -1133,32 +1133,37 @@ func (s *Suite) Test_MkCondChecker_simplify(c *check.C) { nil...) - // FIXME: Syntax error in the generated code. testBeforeAndAfterPrefs( ".if !empty(IN_SCOPE_DEFINED:M)", - ".if ${IN_SCOPE_DEFINED} == ", + ".if ${IN_SCOPE_DEFINED} == \"\"", "NOTE: filename.mk:3: IN_SCOPE_DEFINED can be "+ - "compared using the simpler "+"\"${IN_SCOPE_DEFINED} == \" "+ + "compared using the simpler "+"\"${IN_SCOPE_DEFINED} == \"\"\" "+ "instead of matching against \":M\".", "AUTOFIX: filename.mk:3: "+ "Replacing \"!empty(IN_SCOPE_DEFINED:M)\" "+ - "with \"${IN_SCOPE_DEFINED} == \".", + "with \"${IN_SCOPE_DEFINED} == \\\"\\\"\".", ) - // TODO: Suggest the simpler '${IN_SCOPE_DEFINED:M*.c}'. testBeforeAndAfterPrefs( ".if !empty(IN_SCOPE_DEFINED:M*.c)", - ".if !empty(IN_SCOPE_DEFINED:M*.c)", + ".if ${IN_SCOPE_DEFINED:M*.c}", - nil...) + "NOTE: filename.mk:3: \"!empty(IN_SCOPE_DEFINED:M*.c)\" "+ + "can be simplified to \"${IN_SCOPE_DEFINED:M*.c}\".", + "AUTOFIX: filename.mk:3: "+ + "Replacing \"!empty(IN_SCOPE_DEFINED:M*.c)\" "+ + "with \"${IN_SCOPE_DEFINED:M*.c}\".") - // TODO: Suggest the simpler '!${IN_SCOPE_DEFINED:M*.c}'. testBeforeAndAfterPrefs( ".if empty(IN_SCOPE_DEFINED:M*.c)", - ".if empty(IN_SCOPE_DEFINED:M*.c)", + ".if !${IN_SCOPE_DEFINED:M*.c}", - nil...) + "NOTE: filename.mk:3: \"empty(IN_SCOPE_DEFINED:M*.c)\" "+ + "can be simplified to \"!${IN_SCOPE_DEFINED:M*.c}\".", + "AUTOFIX: filename.mk:3: "+ + "Replacing \"empty(IN_SCOPE_DEFINED:M*.c)\" "+ + "with \"!${IN_SCOPE_DEFINED:M*.c}\".") } func (s *Suite) Test_MkCondChecker_simplify__defined_in_same_file(c *check.C) { diff --git a/pkgtools/pkglint/files/patches.go b/pkgtools/pkglint/files/patches.go index 330308be139..11a46e31a39 100644 --- a/pkgtools/pkglint/files/patches.go +++ b/pkgtools/pkglint/files/patches.go @@ -219,6 +219,9 @@ func (ck *PatchChecker) checkBeginDiff(line *Line, patchedFiles int) { "\tPortability fixes for GCC 4.8 on Linux.", "\tSee https://github.com/org/repo/issues/7", "", + "\t--- old/file", + "\t+++ new/file", + "", "Patches that are related to a security issue should mention the", "corresponding CVE identifier.", "", |