Fix new clippy warnings (#2760)

* Fix new clippy warnings

* Fork serde-hjson and bring in

* Fork serde-hjson and bring in

* Fix clippy lint again
This commit is contained in:
Jonathan Turner
2020-11-22 13:37:16 +13:00
committed by GitHub
parent 63d4df9810
commit 930f9f0063
36 changed files with 5176 additions and 952 deletions

View File

@ -12,6 +12,7 @@ doctest = false
[dependencies]
nu-data = {version = "0.22.0", path = "../nu-data"}
nu-errors = {version = "0.22.0", path = "../nu-errors"}
nu-json = {version = "0.22.0", path = "../nu-json"}
nu-parser = {version = "0.22.0", path = "../nu-parser"}
nu-plugin = {version = "0.22.0", path = "../nu-plugin"}
nu-protocol = {version = "0.22.0", path = "../nu-protocol"}
@ -70,7 +71,6 @@ roxmltree = "0.13.0"
rust-embed = "5.6.0"
rustyline = {version = "6.3.0", optional = true}
serde = {version = "1.0.115", features = ["derive"]}
serde-hjson = "0.9.1"
serde_bytes = "0.11.5"
serde_ini = "0.2.0"
serde_json = "1.0.57"

View File

@ -845,8 +845,8 @@ fn rustyline_hinter(config: &dyn nu_data::config::Conf) -> Option<rustyline::hin
}
fn chomp_newline(s: &str) -> &str {
if s.ends_with('\n') {
&s[..s.len() - 1]
if let Some(s) = s.strip_suffix('\n') {
s
} else {
s
}
@ -863,8 +863,8 @@ pub enum LineResult {
}
pub async fn parse_and_eval(line: &str, ctx: &mut EvaluationContext) -> Result<String, ShellError> {
let line = if line.ends_with('\n') {
&line[..line.len() - 1]
let line = if let Some(s) = line.strip_suffix('\n') {
s
} else {
line
};

View File

@ -63,7 +63,7 @@ pub async fn clip(
let mut first = true;
for i in values.iter() {
if !first {
new_copy_data.push_str("\n");
new_copy_data.push('\n');
} else {
first = false;
}

View File

@ -37,26 +37,26 @@ impl WholeStreamCommand for FromJSON {
}
}
fn convert_json_value_to_nu_value(v: &serde_hjson::Value, tag: impl Into<Tag>) -> Value {
fn convert_json_value_to_nu_value(v: &nu_json::Value, tag: impl Into<Tag>) -> Value {
let tag = tag.into();
let span = tag.span;
match v {
serde_hjson::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag),
serde_hjson::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(&tag),
serde_hjson::Value::F64(n) => UntaggedValue::decimal_from_float(*n, span).into_value(&tag),
serde_hjson::Value::U64(n) => UntaggedValue::int(*n).into_value(&tag),
serde_hjson::Value::I64(n) => UntaggedValue::int(*n).into_value(&tag),
serde_hjson::Value::String(s) => {
nu_json::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag),
nu_json::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(&tag),
nu_json::Value::F64(n) => UntaggedValue::decimal_from_float(*n, span).into_value(&tag),
nu_json::Value::U64(n) => UntaggedValue::int(*n).into_value(&tag),
nu_json::Value::I64(n) => UntaggedValue::int(*n).into_value(&tag),
nu_json::Value::String(s) => {
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag)
}
serde_hjson::Value::Array(a) => UntaggedValue::Table(
nu_json::Value::Array(a) => UntaggedValue::Table(
a.iter()
.map(|x| convert_json_value_to_nu_value(x, &tag))
.collect(),
)
.into_value(tag),
serde_hjson::Value::Object(o) => {
nu_json::Value::Object(o) => {
let mut collected = TaggedDictBuilder::new(&tag);
for (k, v) in o.iter() {
collected.insert_value(k.clone(), convert_json_value_to_nu_value(v, &tag));
@ -67,8 +67,8 @@ fn convert_json_value_to_nu_value(v: &serde_hjson::Value, tag: impl Into<Tag>) -
}
}
pub fn from_json_string_to_value(s: String, tag: impl Into<Tag>) -> serde_hjson::Result<Value> {
let v: serde_hjson::Value = serde_hjson::from_str(&s)?;
pub fn from_json_string_to_value(s: String, tag: impl Into<Tag>) -> nu_json::Result<Value> {
let v: nu_json::Value = nu_json::from_str(&s)?;
Ok(convert_json_value_to_nu_value(&v, tag))
}
@ -96,7 +96,7 @@ async fn from_json(
Err(e) => {
let mut message = "Could not parse as JSON (".to_string();
message.push_str(&e.to_string());
message.push_str(")");
message.push(')');
Some(Err(ShellError::labeled_error_with_secondary(
message,
@ -125,7 +125,7 @@ async fn from_json(
Err(e) => {
let mut message = "Could not parse as JSON (".to_string();
message.push_str(&e.to_string());
message.push_str(")");
message.push(')');
Ok(OutputStream::one(Err(
ShellError::labeled_error_with_secondary(

View File

@ -68,13 +68,12 @@ fn convert_yaml_value_to_nu_value(
Ok(match v {
serde_yaml::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(tag),
serde_yaml::Value::Number(n) if n.is_i64() => {
UntaggedValue::int(n.as_i64().ok_or_else(|| err_not_compatible_number)?).into_value(tag)
UntaggedValue::int(n.as_i64().ok_or(err_not_compatible_number)?).into_value(tag)
}
serde_yaml::Value::Number(n) if n.is_f64() => {
UntaggedValue::decimal_from_float(n.as_f64().ok_or(err_not_compatible_number)?, span)
.into_value(tag)
}
serde_yaml::Value::Number(n) if n.is_f64() => UntaggedValue::decimal_from_float(
n.as_f64().ok_or_else(|| err_not_compatible_number)?,
span,
)
.into_value(tag),
serde_yaml::Value::String(s) => UntaggedValue::string(s).into_value(tag),
serde_yaml::Value::Sequence(a) => {
let result: Result<Vec<Value>, ShellError> = a

View File

@ -264,7 +264,7 @@ fn string_from(input: &[Value]) -> String {
let mut first = true;
for i in input.iter() {
if !first {
save_data.push_str("\n");
save_data.push('\n');
} else {
first = false;
}

View File

@ -304,7 +304,7 @@ fn print_seq(
let before_dec = istr.find('.').unwrap_or(ilen);
if pad && before_dec < padding {
for _ in 0..(padding - before_dec) {
ret_str.push_str("0");
ret_str.push('0');
}
}
ret_str.push_str(&istr);

View File

@ -127,22 +127,22 @@ fn get_output_string(
let mut output_string = String::new();
if !headers.is_empty() {
output_string.push_str("|");
output_string.push('|');
for i in 0..headers.len() {
if pretty {
output_string.push_str(" ");
output_string.push(' ');
output_string.push_str(&get_padded_string(
headers[i].clone(),
column_widths[i],
' ',
));
output_string.push_str(" ");
output_string.push(' ');
} else {
output_string.push_str(headers[i].as_str());
}
output_string.push_str("|");
output_string.push('|');
}
output_string.push_str("\n|");
@ -150,43 +150,43 @@ fn get_output_string(
#[allow(clippy::needless_range_loop)]
for i in 0..headers.len() {
if pretty {
output_string.push_str(" ");
output_string.push(' ');
output_string.push_str(&get_padded_string(
String::from("-"),
column_widths[i],
'-',
));
output_string.push_str(" ");
output_string.push(' ');
} else {
output_string.push_str("-");
output_string.push('-');
}
output_string.push_str("|");
output_string.push('|');
}
output_string.push_str("\n");
output_string.push('\n');
}
for row in rows {
if !headers.is_empty() {
output_string.push_str("|");
output_string.push('|');
}
for i in 0..row.len() {
if pretty {
output_string.push_str(" ");
output_string.push(' ');
output_string.push_str(&get_padded_string(row[i].clone(), column_widths[i], ' '));
output_string.push_str(" ");
output_string.push(' ');
} else {
output_string.push_str(row[i].as_str());
}
if !headers.is_empty() {
output_string.push_str("|");
output_string.push('|');
}
}
output_string.push_str("\n");
output_string.push('\n');
}
output_string

View File

@ -157,7 +157,7 @@ async fn process_row(
None => OutputStream::one(Err(ShellError::labeled_error(
"update could not find place to insert column",
"column name",
field.maybe_span().unwrap_or_else(|| tag.span),
field.maybe_span().unwrap_or(tag.span),
))),
},
Value { value: _, ref tag } => {
@ -166,7 +166,7 @@ async fn process_row(
None => OutputStream::one(Err(ShellError::labeled_error(
"update could not find place to insert column",
"column name",
field.maybe_span().unwrap_or_else(|| tag.span),
field.maybe_span().unwrap_or(tag.span),
))),
}
}

View File

@ -129,7 +129,7 @@ pub fn get_documentation(
let mut long_desc = String::new();
long_desc.push_str(&cmd.usage());
long_desc.push_str("\n");
long_desc.push('\n');
let mut subcommands = vec![];
if !config.no_subcommands {
@ -144,7 +144,7 @@ pub fn get_documentation(
let mut one_liner = String::new();
one_liner.push_str(&signature.name);
one_liner.push_str(" ");
one_liner.push(' ');
for positional in &signature.positional {
match &positional.0 {
@ -175,7 +175,7 @@ pub fn get_documentation(
long_desc.push_str("\nSubcommands:\n");
subcommands.sort();
long_desc.push_str(&subcommands.join("\n"));
long_desc.push_str("\n");
long_desc.push('\n');
}
if !signature.positional.is_empty() || signature.rest_positional.is_some() {
@ -205,7 +205,7 @@ pub fn get_documentation(
long_desc.push_str("\nExamples:");
}
for example in examples {
long_desc.push_str("\n");
long_desc.push('\n');
long_desc.push_str(" ");
long_desc.push_str(example.description);
@ -218,7 +218,7 @@ pub fn get_documentation(
}
}
long_desc.push_str("\n");
long_desc.push('\n');
long_desc
}

View File

@ -50,7 +50,7 @@ impl EnvironmentSyncer {
pub fn did_config_change(&mut self) -> bool {
let config = self.config.lock();
config.is_modified().unwrap_or_else(|_| false)
config.is_modified().unwrap_or(false)
}
pub fn reload(&mut self) {

View File

@ -126,7 +126,7 @@ impl Host for BasicHost {
}
fn width(&self) -> usize {
let (mut term_width, _) = term_size::dimensions().unwrap_or_else(|| (80, 20));
let (mut term_width, _) = term_size::dimensions().unwrap_or((80, 20));
term_width -= 1;
term_width
}

View File

@ -191,8 +191,8 @@ pub fn test_anchors(cmd: Command) -> Result<(), ShellError> {
/// Parse and run a nushell pipeline
fn parse_line(line: &str, ctx: &mut EvaluationContext) -> Result<ClassifiedBlock, ShellError> {
let line = if line.ends_with('\n') {
&line[..line.len() - 1]
let line = if let Some(line) = line.strip_suffix('\n') {
line
} else {
line
};

View File

@ -156,7 +156,7 @@ pub fn scan(paths: Vec<std::path::PathBuf>) -> Result<Vec<crate::commands::Comma
if is_valid_name && is_executable {
trace!(target: "nu::load", "plugin infrastructure -> Trying {:?}", path.display());
build_plugin_command(&path).unwrap_or_else(|_| None)
build_plugin_command(&path).unwrap_or(None)
} else {
None
}

View File

@ -61,7 +61,7 @@ mod tests {
);
//If this fails for you, check for any special unicode characters in your ~ path
assert!(actual.out.chars().filter(|c| c.clone() == '/').count() == 2);
assert!(actual.out.chars().filter(|c| *c == '/').count() == 2);
#[cfg(target_os = "linux")]
assert!(actual.out.contains("home"));
#[cfg(target_os = "macos")]

View File

@ -33,29 +33,17 @@ fn figures_out_intelligently_where_to_write_out_with_metadata() {
#[test]
fn writes_out_csv() {
Playground::setup("save_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"cargo_sample.json",
r#"
{
"package": {
"name": "nu",
"version": "0.14",
"description": "A new type of shell",
"license": "MIT",
"edition": "2018"
}
}
"#,
)]);
sandbox.with_files(vec![]);
let expected_file = dirs.test().join("cargo_sample.csv");
nu!(
cwd: dirs.root(),
"open save_test_2/cargo_sample.json | get package | save save_test_2/cargo_sample.csv",
r#"echo [[name, version, description, license, edition]; [nu, "0.14", "A new type of shell", "MIT", "2018"]] | save save_test_2/cargo_sample.csv"#,
);
let actual = file_contents(expected_file);
println!("{}", actual);
assert!(actual.contains("nu,0.14,A new type of shell,MIT,2018"));
})
}

View File

@ -150,18 +150,24 @@ fn uniq_counting() {
| from json
| wrap item
| uniq --count
| where item == A
| get count
"#
));
let expected = nu!(
assert_eq!(actual.out, "2");
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo '[{"item": "A", "count": 2}, {"item": "B", "count": 1}]'
| from json
echo '["A", "B", "A"]'
| from json
| wrap item
| uniq --count
| where item == B
| get count
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
assert_eq!(actual.out, "1");
}
#[test]