commit: 32b467d9af17e8bd9843e87488f74eba0df748b3 Author: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> AuthorDate: Wed Jun 21 17:57:57 2023 +0000 Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> CommitDate: Wed Jun 21 18:38:40 2023 +0000 URL: https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=32b467d9
RustCheck: check for suboptimal cargo_crate_uris call Resolves: https://github.com/pkgcore/pkgcheck/issues/586 Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org> src/pkgcheck/checks/rust.py | 36 ++++++++++++++++++++++ .../SuboptimalCratesURICall/expected.json | 2 ++ .../RustCheck/SuboptimalCratesURICall/fix.patch | 18 +++++++++++ .../SuboptimalCratesURICall-0.ebuild | 13 ++++++++ .../SuboptimalCratesURICall-1.ebuild | 13 ++++++++ .../SuboptimalCratesURICall-2.ebuild | 13 ++++++++ testdata/repos/standalone/eclass/cargo.eclass | 4 +++ 7 files changed, 99 insertions(+) diff --git a/src/pkgcheck/checks/rust.py b/src/pkgcheck/checks/rust.py index 75197e56..1a5e6770 100644 --- a/src/pkgcheck/checks/rust.py +++ b/src/pkgcheck/checks/rust.py @@ -17,6 +17,20 @@ class SuboptimalCratesSeparator(results.LineResult, results.Warning): return f"line: {self.lineno}: using - as name-version separator in CRATES is suboptimal, use name@version instead" +class SuboptimalCratesURICall(results.LineResult, results.Warning): + """Calling ``cargo_crate_uris`` with ``CRATES`` is suboptimal, use + ``${CARGO_CRATE_URIS}``. + + Calls to ``$(cargo_crate_uris)`` and ``$(cargo_crate_uris ${CRATES})`` are + suboptimal, and can be replaces with ``${CARGO_CRATE_URIS}`` which is + pre-computed, faster and doesn't require sub-shell in global-scope. + """ + + @property + def desc(self): + return f"line: {self.lineno}: calling {self.line!r} is suboptimal, use '${{CARGO_CRATE_URIS}}' for global CRATES instead" + + class RustCheck(Check): """Checks for rust related issues.""" @@ -24,6 +38,7 @@ class RustCheck(Check): known_results = frozenset( { SuboptimalCratesSeparator, + SuboptimalCratesURICall, } ) @@ -44,7 +59,28 @@ class RustCheck(Check): ) return + def _verify_cargo_crate_uris(self, pkg: bash.ParseTree): + for node, _ in bash.cmd_query.captures(pkg.tree.root_node): + call_name = pkg.node_str(node.child_by_field_name("name")) + if call_name == "cargo_crate_uris": + row, _ = node.start_point + line = pkg.node_str(node.parent) + if node.child_count == 1 or ( + node.child_count == 2 + and any( + pkg.node_str(var_node) == "CRATES" + for var_node, _ in bash.var_query.captures(node.children[1]) + ) + ): + yield SuboptimalCratesURICall( + lineno=row + 1, + line=line, + pkg=pkg, + ) + break + def feed(self, pkg: bash.ParseTree): if "cargo" not in pkg.inherited: return yield from self._verify_crates(pkg) + yield from self._verify_cargo_crate_uris(pkg) diff --git a/testdata/data/repos/standalone/RustCheck/SuboptimalCratesURICall/expected.json b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesURICall/expected.json new file mode 100644 index 00000000..a5b21618 --- /dev/null +++ b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesURICall/expected.json @@ -0,0 +1,2 @@ +{"__class__": "SuboptimalCratesURICall", "category": "RustCheck", "package": "SuboptimalCratesURICall", "version": "0", "line": "$(cargo_crate_uris)", "lineno": 13} +{"__class__": "SuboptimalCratesURICall", "category": "RustCheck", "package": "SuboptimalCratesURICall", "version": "1", "line": "$(cargo_crate_uris ${CRATES})", "lineno": 13} diff --git a/testdata/data/repos/standalone/RustCheck/SuboptimalCratesURICall/fix.patch b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesURICall/fix.patch new file mode 100644 index 00000000..6e200a51 --- /dev/null +++ b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesURICall/fix.patch @@ -0,0 +1,18 @@ +diff -Naur standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild fixed/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild +--- standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild ++++ fixed/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild +@@ -10,4 +10,4 @@ DESCRIPTION="Ebuild with suboptimal cargo_crate_uris" + HOMEPAGE="https://github.com/pkgcore/pkgcheck" + SLOT="0" + LICENSE="BSD" +-SRC_URI="$(cargo_crate_uris)" ++SRC_URI="${CARGO_CRATE_URIS}" +diff -Naur standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild fixed/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild +--- standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild ++++ fixed/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild +@@ -10,4 +10,4 @@ DESCRIPTION="Ebuild with suboptimal cargo_crate_uris" + HOMEPAGE="https://github.com/pkgcore/pkgcheck" + SLOT="0" + LICENSE="BSD" +-SRC_URI="$(cargo_crate_uris ${CRATES})" ++SRC_URI="${CARGO_CRATE_URIS}" diff --git a/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild b/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild new file mode 100644 index 00000000..8a8b79d0 --- /dev/null +++ b/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-0.ebuild @@ -0,0 +1,13 @@ +CRATES=" + [email protected] + [email protected] + [email protected] +" + +inherit cargo + +DESCRIPTION="Ebuild with suboptimal cargo_crate_uris" +HOMEPAGE="https://github.com/pkgcore/pkgcheck" +SLOT="0" +LICENSE="BSD" +SRC_URI="$(cargo_crate_uris)" diff --git a/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild b/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild new file mode 100644 index 00000000..1aa95b8f --- /dev/null +++ b/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-1.ebuild @@ -0,0 +1,13 @@ +CRATES=" + [email protected] + [email protected] + [email protected] +" + +inherit cargo + +DESCRIPTION="Ebuild with suboptimal cargo_crate_uris" +HOMEPAGE="https://github.com/pkgcore/pkgcheck" +SLOT="0" +LICENSE="BSD" +SRC_URI="$(cargo_crate_uris ${CRATES})" diff --git a/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-2.ebuild b/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-2.ebuild new file mode 100644 index 00000000..72ef8a24 --- /dev/null +++ b/testdata/repos/standalone/RustCheck/SuboptimalCratesURICall/SuboptimalCratesURICall-2.ebuild @@ -0,0 +1,13 @@ +CRATES=" + [email protected] + [email protected] + [email protected] +" + +inherit cargo + +DESCRIPTION="Ebuild with suboptimal cargo_crate_uris" +HOMEPAGE="https://github.com/pkgcore/pkgcheck" +SLOT="0" +LICENSE="BSD" +SRC_URI="$(cargo_crate_uris ${CRATES} something)" diff --git a/testdata/repos/standalone/eclass/cargo.eclass b/testdata/repos/standalone/eclass/cargo.eclass index f83c98d3..217dc8e8 100644 --- a/testdata/repos/standalone/eclass/cargo.eclass +++ b/testdata/repos/standalone/eclass/cargo.eclass @@ -1 +1,5 @@ # cargo eclass + +CARGO_CRATE_URIS=${CRATES} + +cargo_crate_uris() { :; }
