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

@ -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
}