git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / Atom feed
* [PATCH v1] merge - rename a shadowed variable in cmd_merge
@ 2019-07-05 20:32 Edmundo Carmona Antoranz
  2019-07-08 20:02 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Edmundo Carmona Antoranz @ 2019-07-05 20:32 UTC (permalink / raw)
  To: git; +Cc: Edmundo Carmona Antoranz

variable ret used in cmd_merge introduced in d5a35c114ab was already
a local variable used inside a for loop inside the function.

for-local variable is being renamed to ret_try_merge to avoid shadow.
---
 builtin/merge.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 6e99aead46..972b6c376a 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1587,7 +1587,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		oidclr(&stash);
 
 	for (i = 0; i < use_strategies_nr; i++) {
-		int ret;
+		int ret_try_merge;
 		if (i) {
 			printf(_("Rewinding the tree to pristine...\n"));
 			restore_state(&head_commit->object.oid, &stash);
@@ -1601,26 +1601,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		 */
 		wt_strategy = use_strategies[i]->name;
 
-		ret = try_merge_strategy(use_strategies[i]->name,
-					 common, remoteheads,
-					 head_commit);
-		if (!option_commit && !ret) {
+		ret_try_merge = try_merge_strategy(use_strategies[i]->name,
+						common, remoteheads,
+						head_commit);
+		if (!option_commit && !ret_try_merge) {
 			merge_was_ok = 1;
 			/*
 			 * This is necessary here just to avoid writing
 			 * the tree, but later we will *not* exit with
 			 * status code 1 because merge_was_ok is set.
 			 */
-			ret = 1;
+			ret_try_merge = 1;
 		}
 
-		if (ret) {
+		if (ret_try_merge) {
 			/*
 			 * The backend exits with 1 when conflicts are
 			 * left to be resolved, with 2 when it does not
 			 * handle the given merge at all.
 			 */
-			if (ret == 1) {
+			if (ret_try_merge == 1) {
 				int cnt = evaluate_result();
 
 				if (best_cnt <= 0 || cnt <= best_cnt) {
-- 
2.22.0.214.g8dca754b1e


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] merge - rename a shadowed variable in cmd_merge
  2019-07-05 20:32 [PATCH v1] merge - rename a shadowed variable in cmd_merge Edmundo Carmona Antoranz
@ 2019-07-08 20:02 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2019-07-08 20:02 UTC (permalink / raw)
  To: Edmundo Carmona Antoranz; +Cc: git

Edmundo Carmona Antoranz <eantoranz@gmail.com> writes:

> variable ret used in cmd_merge introduced in d5a35c114ab was already
> a local variable used inside a for loop inside the function.

Strictly speaking, there was a local variable 'ret' inside for loop,
which is unrelated to the variable introduced by the said commit.
The only resemblance was that they happen to share the same name.
So "was already a local variable" is not quite right, and made my
reading hiccup.

> for-local variable is being renamed to ret_try_merge to avoid shadow.

Is this really a problem that needs to be changed?  What compiler
is having trouble with the code?

I am reasonably negative on this change.  But as you seem to be a
new contributor, let me grab this opportunity to comment on other
aspects of the patch.

> ---

Missing sign-off.

In the proposed commit log message body, write full sentences just
like normal English, e.g. a sentence begins with a capital letter,
etc.

The usual pattern used in our log messages is first to give an
observation of the current state and state what the problem is,
and then write orders you give to the codebase to be like so to fix
the problem, e.g.

	The commit d5a35c11 ("Copy resolve_ref() return value for
	longer use", 2011-11-13) introduced a variable 'ret' to
	cmd_merge() to keep the final return value from the
	function.  There however was an unrelated variable that is
	local to a for loop that shared the same name.  Because the
	statements inside of the loop do not have enough information
	to decide the final outcome of the function, there is no
	need for the outer 'ret' to be visible to them, which is
	a perfectly good reason to use the "shadowing" technique.

	Rename the local variable used inside the for loop to avoid
	warnings when compiled with -Wshadow; this will expose the
	outer 'ret' to the statements in the loop, allowing them to
	mistakenly making an assignment to it, though.

is how I would describe this change.  As you can see, this trades
"make -Wshadow less noisy" with "make it easier to make mistakes"
and I am not sure if it is a good trade-off.

> diff --git a/builtin/merge.c b/builtin/merge.c
> index 6e99aead46..972b6c376a 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -1587,7 +1587,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
>  		oidclr(&stash);

Interesting.

All assignments to ret up to this point are all followed by "goto
done" to jump over the "for" loop we are looking at this patch.
So we know that when the control reaches at this point, ret has its
initial value 0.

So an alternative approach would be to just ...

>  	for (i = 0; i < use_strategies_nr; i++) {
> -		int ret;
> +		int ret_try_merge;

... drop this local variable declaration and let it contaminate the
outer 'ret', and then after the loop is done, assign 0 to ret.  That
would squelch "-Wshallow" and at the same time makes sure that the
loop won't corrupt the "proposed final outcome" stored in 'ret'.

Quite honestly, I think the easiest "solution" would be not to use
"-Wshadow" in your compilation.  Thsi file has a handful other
instances of variable shadowing, and most of them do not look
confusing or problematic.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-05 20:32 [PATCH v1] merge - rename a shadowed variable in cmd_merge Edmundo Carmona Antoranz
2019-07-08 20:02 ` Junio C Hamano

git@vger.kernel.org list mirror (unofficial, one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.org/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox