Sitelet https://web.archive.org/web/20260624111036/https://github.com/RustPython/RustPython/pull/5442
Skip to content

Miscellaneous cli-related parity fixes#5442

Merged
coolreader18 merged 2 commits into
RustPython:mainfrom
coolreader18:cli-fixes
Dec 4, 2024
Merged

Miscellaneous cli-related parity fixes#5442
coolreader18 merged 2 commits into
RustPython:mainfrom
coolreader18:cli-fixes

Conversation

@coolreader18

@coolreader18 coolreader18 commented Nov 13, 2024 •

Copy link
Copy Markdown
Member

Part of this is taken from #5414.

@coolreader18 coolreader18 force-pushed the cli-fixes branch 3 times, most recently from decf6a8 to cccbd2e Compare November 14, 2024 23:25
Comment thread src/lib.rs
debug!("Running command {}", command);
vm.run_code_string(scope, &command, "<stdin>".to_owned())?;
vm.run_code_string(scope.clone(), &command, "<stdin>".to_owned())
.map(drop)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a common convention to drop value from Result? It looks like drop is important logic here to me. If not, a weak suggestion.

Suggested change
.map(drop)
.map(|_| ())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen .map(drop) a decent amount, yeah.

Comment thread src/settings.rs
Comment on lines +349 to +354
env::var_os(name).filter(|v| !v.is_empty()).map(|value| {
value
.to_str()
.and_then(|v| v.parse::<u8>().ok())
.unwrap_or(1)
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
env::var_os(name).filter(|v| !v.is_empty()).map(|value| {
value
.to_str()
.and_then(|v| v.parse::<u8>().ok())
.unwrap_or(1)
})
let value_str = env::var_os(name).filter(|v| !v.is_empty())?;
let value = value_str
.to_str()
.and_then(|v| v.parse::<u8>().ok())
.unwrap_or(1);
Some(value)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given there's 2 layers of Option here, I think I'd like to keep the logic as it is - looking at the suggested version my instinct was to change it to .to_str()?.parse().unwrap_or(1), but that's different. I think the explicit map() makes more clear the control flow and conditions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 349 and the following lines are unrelated logics. early return reveals line 350 is independent to 349 and code readers can forget about 349 when going to 350.
Using map make readers essentially follow the full logic without benefit.

@coolreader18 coolreader18 merged commit 848db34 into RustPython:main Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants